Hello Readers! In this blog we will be discussing about the different identifiers. What is the difference between them and when to use which identifier? First lets start with the const.
Const : is used to declare a variable whose value cannot be changed after it has been initialized. The value of the variable remains unchanged during re-assignment and also it can not be redeclared.
// define MYVARIABLE1 as a constant and assign value 10 to it
const MYVARIABLE1 = 10;
// following line will throw an error
MYVARIABLE1 = 20;
// will print 10 as output
console.log( The value of MYVARIABLE1 is: + MYVARIABLE );
// redeclaration of a constant will throw an error
const MYVARIABLE1 = 20;
var : variable declared with the var keywords can be redeclared and re-assigned, that we can change the value of the variable which is declared using the var keyword.
var x =10;
var y =20;
var sum = x+y;
console.log(sum); // prints 30 as output
x= 30; // redeclaration of variable x
sum =x+y;
console.log(sum); // print 50 as output
Happy Coding :)
0 Comment(s)