Answers for "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
5

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.
Posted by: Guest on June-29-2020
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
2

var or const in javascript

// var declares a variable, meaning its value will vary. 
// const declares a constant, meaning its value will remain 
// consistant and not change. 
// If your variable changes throughout the program or website, 
// declare it using a var statement. 
// Otherwise, if its value does not change, declare it using 
// a const statement. 

const myConst='A const does not change.';

var myVar='A var does change.';

var myVar=2;
Posted by: Guest on July-09-2020

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

Browse Popular Code Answers by Language