Answers for "what is the difference between const and var in javascript"

2

js var vs const

//var variables can be re-declared and updated
    var greeter = "hey hi";
    var greeter = "say Hello instead";
//it becomes a problem when you do not realize that a variable 
//greeter has already been defined before.

//const declarations are block scoped
//const cannot be updated or re-declared
	const greeting = "say Hi";
	greeting = "say Hello instead";// error: Assignment to constant variable. 

	const greeting = "say Hi";
	const greeting = "say Hello instead";// error: Identifier 'greeting' has already been declared
Posted by: Guest on August-01-2021
0

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.
Posted by: Guest on February-12-2021

Code answers related to "what is the difference between const and var in javascript"

Browse Popular Code Answers by Language