1.

How to shuffle an array in JavaScript?

Answer»

The var and let are used to declare a variable in JavaScript. 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 the value if i variable as 2:

Example of var

Let us see the same example and place var in place of let to work with variables:

<!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 the value of i variable as 10:



Discussion

No Comment Found