February 11, 20206 yr This article urges people not to use var in JavaScript and instead use const and let for declaring variables. What do you think about that? https://levelup.gitconnected.com/stop-using-var-to-declare-variables-in-javascript-6c0caec16f43
February 11, 20206 yr I pretty much use const and let in all of my JS code, but I'd recommend understanding how scope works before you use any of them, and the pros and cons of each keyword. You'll come across var a fair amount still, so it's important to understand what the rules are for when you do come across it. What you should definitely avoid is omitting the var keyword completely. Doing so will create a global variable that can be accessed and changed anywhere and this is a potential cause for bugs and difficult to debug code.
February 12, 20206 yr let and const are two of many features used in modern JavaScript. Typically you'd need to compile (often referred to as transpiling) this code AKA ESNext/ES6 into regular JavaScript (ES5) to be fully browser compatible, some features are already implemented but some are not so I'd recommend using a tool like Babel to do this job for you.@Jack Why do you suggest that ommiting var completely is a bad idea? If you are compiling down there is really no benefit to using var these days. I've used const and let exclusively for the past 3-4 years, if you're unfortunate enough to be working on REALLY old projects then it's good to know how var works. All of these keywords have what is known as lexical scope which means functions declared within a block can access variables outside of the function body, variables declared inside the function are not available outside of the function unless you leverage closure (returning those variables in some way) var declarations will be hoisted to the top of the function they are declared in and can be reassigned to let declarations are only hoisted to the top of the block they are declared in (block being curly braces) and can be reassigned const declarations are also hoisted to the top of the block and cannot be reassigned to (often confused to be immutable which isn't the case) If you are using ES6 with Babel then I'd always go with const unless you have a good reason to re-assign (which shouldn't be that often) in which case use let.
February 12, 20206 yr 2 hours ago, rbrtsmith said: Why do you suggest that ommiting var completely is a bad idea? If you are compiling down there is really no benefit to using var these days. I've used const and let exclusively for the past 3-4 years, if you're unfortunate enough to be working on REALLY old projects then it's good to know how var works. I use const basically every time, with a few exceptions. What I mean by omitting the var, let, or const keyword is leaving the keyword off entirely. If you don't add any keyword, you create a global variable that can leak data and be reassigned, and that's absolutely not what you want. // without var, let or const animal = 'cat' function printAnimal() { animal = 'dog' console.log(animal) // dog } printAnimal() console.log(animal) // dog // with var var animal = 'cat' function printAnimal() { var animal = 'dog' console.log(animal) // dog } printAnimal() console.log(animal) // cat If you intend on using only modern JS I still think you should know how var works, even if you don't intend on using it going forward. It's still used and it's highly unlikely you won't come across it somewhere in the wild. I also think it's better to understand why some people don't use var anymore, rather than just accepting it as a given, and you can only really do that if you understand the pros and cons of all 3 keywords. Fortunately, the rules for all of them are straight forward to understand, so there's not much additional work required to do so.
February 13, 20206 yr Oh of course you want the keyword, I guess not everybody here is using a linter as if you are something like that'd be caught immediately. True you want to know how var works, but even if you didn't and came across it you can spend all of about 2 minutes looking at MDN to find out what it does. I've not seen var being used anywhere for at least a few years though I'm fortunate enough to not have to touch very legacy codebases.
February 13, 20206 yr 3 minutes ago, rbrtsmith said: Oh of course you want the keyword, I guess not everybody here is using a linter as if you are something like that'd be caught immediately. True you want to know how var works, but even if you didn't and came across it you can spend all of about 2 minutes looking at MDN to find out what it does. I've not seen var being used anywhere for at least a few years though I'm fortunate enough to not have to touch very legacy codebases. It's being used without you knowing in places like Babel, React etc, but yeah I'd default to const nowadays and let the tooling handle it.
February 13, 20206 yr 4 hours ago, Jack said: It's being used without you knowing in places like Babel, React etc, but yeah I'd default to const nowadays and let the tooling handle it. I guess what I meant was that I never have to interact with var. What stuff compiles down to doesn't really matter as it's not meant for human to read it. The same way there's little point to being able to read binary code as a web developer because we'll never read what our code is eventually compiled to.
Create an account or sign in to comment