When we use const variable? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

When we use const variable?

explain with a example

12th Jan 2021, 5:53 PM
Sajid Ali
Sajid Ali - avatar
5 Answers
+ 4
ASJAD🔹 const is not similar to let. You can change the value of let but you can't change the value of const. Sajid Ali When you don't want to change the value of a variable then you should use const. For example: const x = 10 let y = 20 y = 30 //it will not give error x = 20 //it will give error console.log(y) console.log(x) Try above code in Code Playground and see the difference.
12th Jan 2021, 6:17 PM
A͢J
A͢J - avatar
+ 2
Actually, it is not variable it's constant value and you can use them to declare higher order function * aka lambda some Times it is called "arrow function", none mutable values ( which you no longer dare to change their value)
12th Jan 2021, 5:57 PM
Ananiya Jemberu
Ananiya Jemberu - avatar
+ 2
ASJAD🔹 You can say var is similar to let but let is blocked scope means we cannot access the value of let outside the block. You can try below example in Code Playground { var x = 30 let y = 20 } console.log(x) console.log(y)// gives error
12th Jan 2021, 6:25 PM
A͢J
A͢J - avatar
+ 1
ASJAD🔹 bro.. local variable can not be access from outside block
12th Jan 2021, 6:27 PM
Sajid Ali
Sajid Ali - avatar
+ 1
I Am AJ ! It mean that,, Const variable can not be override the value,,, in respect of local block and global block And Let variable can be override the value in respect of local block and global block
12th Jan 2021, 6:28 PM
Sajid Ali
Sajid Ali - avatar