+ 2
In javascript for variable declaration we have 3 keywords that is :- var, let, const.
When to use which keyword for declaring variable?
3 Réponses
+ 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.
+ 3
Please search for answers to your question first.
See if these help.
https://www.sololearn.com/discuss/2944013/?ref=app
https://www.sololearn.com/discuss/1440112/?ref=app
https://www.sololearn.com/discuss/1911206/?ref=app
https://www.sololearn.com/discuss/1491784/?ref=app
https://www.sololearn.com/discuss/2958499/?ref=app
+ 2
Thanks, Now I understand well.