- 2
Explain me let and const in JavaScript
If you explain me this my 30% salary is yours
6 Réponses
+ 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.
+ 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
+ 3
Webbber
First give me 30% then I will explain.
+ 2
const mean that ur variable will not be reassigned
let works just like var(there are differences though, google it)
+ 2
Webbber
Here is my explanation....lol
- 2
🅘🅜 🅰🅹 !!! Here's your 30% man...lol