Explain me let and const in JavaScript | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 2

Explain me let and const in JavaScript

If you explain me this my 30% salary is yours

27th Mar 2021, 6:45 AM
Hritik Ulhas Mumbarkar
Hritik Ulhas Mumbarkar - avatar
6 Answers
+ 1
Firstly, there are some differences in hoisting between var on the one hand and let/const on the other. Take a look over at https://developer.mozilla.org/en-US/docs/Glossary/Hoisting for an explanation, but generally "hoisting" has to do with trying to access a variable before it's created: console.log(x);//error! let x = 4; versus console.log(y);//undefined! var y = 4; In other words, with var, the variable declaration (that it exists) gets shoved to the top of the script, but its value (4) doesn't. With let, neither gets hoisted. As for the difference between const and let, saying that const is for a variable "whose value you don't want to change" is... not entirely correct. Instead, a variable declared with const cannot be *reassigned*. We can, however, change sub-properties of that variable. Take a look: const myNum = 5; myNum = 6;//Error! We're attempting to reassign the "whole" myNum var const myObj = { name: 'HealyUnit'}; myObj = 'something else';//Again, error! Attempting to reassign a constant; myObj.name = "Webbber";//fine! With let, on the other hand, we can reassign at will. The rule of thumb is generally to use const unless you *know* you'll need to reassign the variable.
28th Mar 2021, 8:14 PM
HealyUnit
HealyUnit - avatar
+ 3
https://code.sololearn.com/cF94O0dgbJqD/?ref=app You can use let a part of block only ,var you access anywhere if it's globally available !! Const is use for constant, whose value you don't want to change. [ Note : It's a nice practice to write constants in upper case ] Eg, const PI = 3.14 console.log(PI) YouTube Video for explanation - https://youtu.be/q8SHaDQdul0 And as Lily Madam told that use Google for explore more
27th Mar 2021, 7:09 AM
Abhiyantā
Abhiyantā - avatar
+ 3
Webbber First give me 30% then I will explain.
27th Mar 2021, 8:40 AM
A͢J
A͢J - avatar
+ 2
const mean that ur variable will not be reassigned let works just like var(there are differences though, google it)
27th Mar 2021, 6:51 AM
durian
durian - avatar
+ 2
Webbber Here is my explanation....lol
27th Mar 2021, 4:37 PM
A͢J
A͢J - avatar
- 2
🅘🅜 🅰🅹 !!! Here's your 30% man...lol
27th Mar 2021, 1:26 PM
Hritik Ulhas Mumbarkar
Hritik Ulhas Mumbarkar - avatar