var vs let vs const in JavaScript

If you try to access a variable declared with let before it’s declared, instead of getting undefined (like with those variables declared with var), you’ll get a ReferenceError.function discountPrices (prices, discount) { console.log(discounted) // ❌ ReferenceError let discounted = [] for (let i = 0; i < prices.length; i++) { let discountedPrice = prices[i] * (1 – discount) let finalPrice = Math.round(discountedPrice * 100) / 100 discounted.push(finalPrice) } console.log(i) // 3 console.log(discountedPrice) // 150 console.log(finalPrice) // 150 return discounted}var VS letvar: function scoped undefined when accessing a variable before it's declaredlet: block scoped ReferenceError when accessing a variable before it's declaredlet VS constNow that you understand the difference between var and let, what about const?.Turns out, const is almost exactly the same as let..However, the only difference is that once you’ve assigned a value to a variable using const, you can’t reassign it to a new value.let name = 'Tyler'const handle = 'tylermcginnis'name = 'Tyler McGinnis' // ✅handle = '@tylermcginnis' // ❌ TypeError: Assignment to constant variable.The take away above is that variables declared with let can be re-assigned, but variables declared with const can’t be.Cool, so anytime you want a variable to be immutable, you can declare it with const..Well, not quite..Just because a variable is declared with const doesn’t mean it’s immutable, all it means is the value can’t be re-assigned..Here’s a good example.const person = { name: 'Kim Kardashian'} = 'Kim Kardashian West' // ✅person = {} // ❌ Assignment to constant variable.Notice that changing a property on an object isn’t reassigning it, so even though an object is declared with const, that doesn’t mean you can’t mutate any of its properties..It only means you can’t reassign it to a new value.Now the most important question we haven’t answered yet, should you use var, let, or const?.The most popular opinion, and the opinion that I subscribe to, is that you should always use const unless you know the variable is going to change..The reason for this is by using const, you’re signalling to your future self as well as any other future developers that have to read your code that this variable shouldn’t change..If it will need to change (like in a for loop), you should use let.So between variables that change and variables that don’t change, there’s not much left..That means you shouldn’t ever have to use var again.Now the unpopular opinion, though it still has some validity to it, is that you should never use const because even though you’re trying to signal that the variable is immutable, as we saw above, that’s not entirely the case..Developers who subscribe to this opinion always use let unless they have variables that are actually constants like _LOCATION_ = ….So to recap, var is function scoped and if you try to use a variable declared with var before the actual declaration, you’ll just get undefined.. More details

Leave a Reply