A note about const
, let
, and var
in JavaScript
#468
anna-murphy
started this conversation in
Tips and Tricks
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
JavaScript has a few different ways to declare variables. Unlike other typed languages (Java, C, etc), variables are not declared with an explicit type. Instead, you'll use one of three keywords:
const
,let
, orvar
. In JavaScript, each of the keywords are valid, but do slightly different things.const
is the most general purpose keyword for declaring variables. It declares a variable, and sets it as immutable (hence, "const" as "constant"). As a result, once a variable is declared asconst
, you won't be able to update it.Similarly, attempting to redeclare a variable with the same identifier in the same scope will also fail (more on scope in a little bit).
The immutability of a
const
variable is limited only to reassignment (eg,counter = newNumber
). If you have an object or an array, you'll still be able to modify fields on that object or add elements to the array.In JavaScript, it's considered best practice to use
const
as the default keyword. If you need your variable to specifically be mutable, then you can use a different keyword.By contrast,
let
is used to initialized variables that can change.Similar to
const
, variables declared withlet
cannot be redeclared either.While it might seem like a good idea to default to using
let
whenever you're declaring a variable, your code will probably have fewer errors if you're purposeful about what variables you make mutable, and which you don't. And often times, there are more idiomatic ways to perform some action in JavaScript than by declaring a mutable variable. For example, these are both ways to sum a list:Lastly,
var
is the traditional keyword to declare variables in JavaScript. But, certain niche behaviors ofvar
means that it's best not to use unless you specifically want to leverage these behaviors.var
works very similarly tolet
in that the declared variable can be mutated in some way.But additionally, you can redeclare a variable with
var
, resetting the prior value:Additionally, where
const
andlet
are block scoped,var
is globally scoped or function scoped. Here, "scoped" refers to where a declared variable is accessible from. For example, if you declare a variable inside a function, you shouldn't expect it to be accessible outside that function.The reason that
foo
is or isn't accessible in certain contexts are defined by the variable's scoping rules.const
andlet
are block scoped, where a "block" is any code contained within a set of curly braces (like a function, loop, orif
statement). Block can also be nested (like functions that contain loops, or nestedif
statements). When a variable is block scoped, it's then available in its current block or any nested child block.By contrast,
var
s are "hoisted" to the top most function or global scope. So even if you declare it in a way that would normally limit it to being scoped down to a certain section of code, it will unexpectedly move around and be accessible in multiple places. Combine this with the strange re-declaration behavior described above and you'll run into some truly strange bugs. For example:This may seem rather contrived, but in a world of bundlers and linked files, it's easier than you'd expect to run into errors. As such, it's generally not recommended to ever use
var
when you're declaring a variable unless you're doing so with those quirks in mind.tl;dr
When it comes to
const
,let
, andvar
, you should really never usevar
if you can avoid it. Default to usingconst
, and then only if you need your variable specifically to be mutable, uselet
.Beta Was this translation helpful? Give feedback.
All reactions