difference between var const and let in javascript code example

Example 1: Difference between let and var in javascript

let a = 'hello'; // globally scoped
var b = 'world'; // globally scoped
console.log(window.a); // undefined
console.log(window.b); // 'world'
var a = 'hello';
var a = 'world'; // No problem, 'hello' is replaced.
let b = 'hello';
let b = 'world'; // SyntaxError: Identifier 'b' has already been declared

Example 2: what is the difference between let and const in javascript

The difference is that with const you can only only assign a value to a variable
once, but with let it allows you to reassign after it has been assigned.

Example 3: difference between var let and const in javascript with example

//functional scope 
 var a; // declaration
 a=10; // initialization; 
//global scope
// re-initialization possible
 let a;//only blocked scope & re-initialization possible
 a=10;
let a =20;
if(true){
  let b =30;
}
console.log(b); // b is not defined
const // const also blocked scope,Re-initialization and re-declaration not possible
const a; // throws error {when we declaring the value we should assign the value.
const a =20;
if(true){
  const b =30;
}
console.log(b); // b is not defined
console.log(a); // no output here because code execution break at leve b.

Tags:

Misc Example