Variables in JavaScript

Michael Causey
2 min readJan 18, 2021

Beginning to work with variables can be confusing for many new programmers, and with recent changes to the JavaScript language, it can cause some difficulty in learning how to work with variables the first time. Hopefully this piece will help sort out some of the confusion.

Var, Let, and Const

First, let’s start with the difference between declaring variable. Prior to 2015, the only way to declare a variable was using the term “var”. However, with the incorporation of ES6 and the changes it brought to the JavaScript, using the terms “let” and “const” are included as ways to declare variables. These new terms have difference attributes depending on their use. When using “let” to declare and define your variable, it allows the definition to change within a restricted scope. “Const”, on the other hand, is used when the variable cannot be changed later, most useful when defining global variables, for example.

Quick note: Older technology and legacy code bases may continue to use “var” to declare all variables, be sure to check with any superiors or more experienced programmers before making any large changes to existing bases.

Declaring vs Defining

The second piece to this is defining the variable. Now, any time a variable is created, it starts with being declared, or in essence, identified. For example, consider code below.

For the sake of instruction, this has been broken down into two lines. The first would be an example of the variable being declared. The second line would be the variable being defined, or given a value. Usually, this would be simplified as “let variable = value”. Note that in the case of JavaScript, this is a process that can indeed be separated, thought this may not be the case with all languages, check the language’s documentation for any confusion.

--

--

Michael Causey

Flatiron School DC. Learning to code one day at a time.