/Programming


Learn to develop things, get out of debt, find answers and take part in coding and programming challenges.


Members: 8
Join


Moderated by: mozzapp
up
3
up
Manon_code 1783540168 [Programming] 1 comments
Look, if you've spent any time messing around with JavaScript or TypeScript, you've almost certainly found yourself staring at the screen wondering why on earth a variable changed its value out of nowhere. Most of the time, the blame lies squarely on how we choose to declare things, and the truth is that the behavior of var, let, and const confuses a lot of people at first. The var keyword is the oldest of them all, and honestly, nowadays there’s barely any reason to use it. Its big issue is that it doesn’t care about blocks like ifs or for loops, it only respects scope if it's inside a function. If you create something like `var hero = "Batman"` and right after write `var hero = "Superman"`, JavaScript just lets you do it without a single warning, overwriting the value. It’s even worse when you make some `if (true)` and declare `var secret = "Bruce Wayne"` inside it. Because var doesn't care about blocks, that variable leaks right out of the if, and you can run a `console.log(secret)` in another part of the code and the value will be sitting right there exposed. You can imagine the amount of weird bugs this caused in large projects by allowing that redeclaration and having that global or function scope that leaks so easily. To fix all that mess, let came along, bringing much stricter rules for scope and reassignment. It works the way most people actually expect a variable to work, meaning it respects the boundaries of those curly braces where it was created and won't accept being redeclared in the same scope. You can have a `let points = 10` and then do `points = 15` to change the value all you want, but if you try to write `let points = 20` right after, the system fails on the spot. And if you create a `let bonus = 5` inside an if, that bonus dies the moment the if ends. If you try to call that bonus variable outside, you’ll get a reference error saying it’s not defined, which is great for keeping the code clean. Then we have const, which people usually say is for creating constants that don't change. Const shares the exact same block logic as let and also refuses redeclaration, but it goes a step further and won't let you do any reassignment. If you define `const pi = 3.1415`, you can't try to stuff a `pi = 3.14` in there further down the line because the system blocks it. Except there's a detail that catches a lot of people off guard at the beginning. Saying const is totally immutable is kind of a lie. If you create something like `const user = { name: "Carlos", age: 30 }`, you can't replace the whole object by doing `user = { name: "Ana" }`, but you can perfectly fine do `user.age = 31`. Const only guarantees that the drawer where you stored things remains the same, it doesn't stop you from messing with the content inside it. The same thing happens with arrays, where you can add or remove items without any issues. Another technical detail that all three share but in different ways is hoisting, which is that tendency JavaScript has to pull declarations to the top of the code before executing it. The var keyword undergoes hoisting and gets a value of undefined if you try to use it before its line, while let and const also undergo hoisting but leave the variable in an inaccessible temporal dead zone, throwing an immediate error if you try to touch them ahead of time. In TypeScript, the logic is exactly the same because underneath it's all just JavaScript anyway, the only real difference is that the editor is much more nitpicky. Instead of finding out about the error only when the app is running in the browser, TypeScript underlines the code in red right away while you're writing and explicitly warns you if you're trying to mess with a constant or duplicate a variable that should be unique to that scope. When in doubt, I think the best approach is to just use const by default for pretty much everything you write. If you realize the value actually needs to change somewhere, like in a counter or a loop, you change it to let. As for var, you can just pretend it doesn't exist anymore, at least in my experience that saves a ton of headaches.
up
1
up
x1012 1783540637
something truly simple, but which has always been a puzzle to me

A social news and discussion community