Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,851 questions

51,772 answers

573 users

What is the difference between var and let in JavaScript

4 Answers

0 votes
// one i: start as 'abc', finish as 5

var i = 'abc'; 
for (var i = 0; i < 5; i++) {
    console.log("i = ", i); 
}

console.log(i);

// two j: first j is 'abc', second j is 0 to 5 in for block  

let j = 'abc'; 
for (let j = 0; j < 5; j++) {
    console.log("j = ", j); 
}

console.log(j);



     
/*
run:
   
i =  0
i =  1
i =  2
i =  3
i =  4
5
j =  0
j =  1
j =  2
j =  3
j =  4
abc
 
*/

 



answered Mar 6, 2020 by avibootz
0 votes
var a = 5;
var a = 30;
console.log(a); // 30


let b = 5;
let b = 30; // SyntaxError: Identifier 'b' has already been declared

console.log(b);



     
/*
run:
   
let b = 30; // SyntaxError: Identifier 'b' has already been declared
 
*/

 



answered Mar 6, 2020 by avibootz
0 votes
var a = 5;
if (true)
    var a = 30; // Same variable: a
console.log(a); // 30


let b = 5;
if (true)
    let b = 30; // SyntaxError: Identifier 'b' has already been declared

console.log(b);



     
/*
run:
   
let b = 30; // SyntaxError: Identifier 'b' has already been declared
 
*/

 



answered Mar 6, 2020 by avibootz
0 votes
var a = 7;
if (true) {
    var a = 30; // Same variable: a
}
console.log(a); // 30


let b = 7;
if (true) {
    let b = 30; 
}

console.log(b);



     
/*
run:
   
30
7
 
*/

 



answered Mar 7, 2020 by avibootz
...