0
Q:

var or const in javascript

Const vs Let vs Var

const pi = 3.14

pi = 1 
cannot do this becuase with const you cannot change the value

_____________________________________


Let --> is block level 

for(let i = 0; i < 3; i++) {
console.log(i) --> it will console here
}
console.log(i) ---> Not here


---------------------------------------

Var is for variables available to the entire function 


for(var j = 0; j < 3; j++) {
console.log(j) --> it will console here
}
console.log(j) ---> it will console here
7
// 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;
1

New to Communities?

Join the community