InterviewSolution
| 1. |
How to compare two dates in JavaScript? |
|
Answer» The new way to define a variable in JavaScript is using the let statement. The ECMAScript 2015 introduced it. As you know that variables can also be DECLARED with var, but the usage of var are scoped to the function block level. Declare variables that are limited in scope to the block, statement, or expression using the let. Redeclaring a variable inside a block will not redeclare the variable outside the block. Example of let Let us see an example of let. The variable declared in the loop does not redeclare the variable outside the loop: <!DOCTYPE html> <html> <BODY> <h3>JavaScript let</h3> <p ID="myid"></p> <script> let i = 2; for (let i = 0; i < 10; i++) { document.write(i); } document.getElementById("myid").innerHTML = i; </script> </body> </html>The output displays 2 as the variable i value: Example of var Let us see the same example using var this time instead let keyword: <!DOCTYPE html> <html> <body> <h3>JavaScript var</h3> <p id="myid"></p> <script> var i = 2; for (var i = 0; i < 10; i++) { document.write(i); } document.getElementById("myid").innerHTML = i; </script> </body> </html>The output displays 10 as the value of variable i: |
|