In javascript for variable declaration we have 3 keywords that is :- var, let, const. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

In javascript for variable declaration we have 3 keywords that is :- var, let, const.

When to use which keyword for declaring variable?

27th Sep 2022, 7:06 AM
Hemant Kumbhalkar
Hemant Kumbhalkar - avatar
3 Answers
+ 5
"var" is variable scope. It's global. The purpose is to avoid undefined errors. As variables will be defined first no matter where you put them. This is however a old and discouraged practice that will result in a lot of unexpected behaviour. Just avoid it. "let" is a mutable container limited to block scope. You can reassign a value to it. And that value will only be accessible inside the blackest that it was declared in. "const" is for immutable data. It's block scope just like "let". However, you can't reassign a value to it. Also, the value needs to be assigned during declaration. In short. Avoid "var". Use "let" if you want the value to change and "const" if you don't.
27th Sep 2022, 7:16 AM
Mustafa A
Mustafa A - avatar
+ 2
Thanks, Now I understand well.
27th Sep 2022, 8:24 PM
Hemant Kumbhalkar
Hemant Kumbhalkar - avatar