DEV Community

Cover image for Variables - JavaScript Core Concepts
Angeline Wang
Angeline Wang

Posted on • Updated on

Variables - JavaScript Core Concepts

"JavaScript Core Concepts" is a 5-Section Series consisting of:

Section 1 Data Types & Variables
Part 1 Variables
Part 2 Primitive Data Types
Part 3 Complex/Composite Data Types

Section 2 Functions & Methods
Section 3 Control Flow & Conditional Statements
Section 4 Past, Present & Future
Section 5 Data Structures & Algorithms

The most important thing to learn about any new programming language is its Data Types & Variables. And that is what we are starting off with.

...

JavaScript Variables

What is the purpose of variables?

JavaScript variables are used to hold data in memory, so that you can access and manipulate it later on.

How can you define variables?

There are 3 ways of defining a JavaScript variable:

  1. var
    This may or may not be reassigned, and may or may not be used for an entire function, or just for the purpose of a block or loop. This is the weakest method, which should not be used, but is still valid.

  2. let
    Used when you need to re-assign the variable later on, and is particularly useful in loops. It signals that the variable will only be used within the block that it’s defined in, and this may not always be the entire containing function.

  3. const
    Used when the variable does not need to be re-assigned later on, and means that the variable remains constant throughout the script/code block. This does not mean that it has immutable values, as an object can still have its properties mutated.

Which should you use?

When in doubt, use const, as this allows the usage to be as clear as possible. If later on, you realise that you need to modify the variable, you can always go back and change the definition to let.

let tends to be used for loops or mathematical algorithms to take the place of one thing. This means that it is possible to deallocate the value by unsetting it. For example, it can be used for a counter in a loop or a value swap in an algorithm.

How does block scope interact with variables in JS?

All code within curly braces {} is a code block. If a variable is created with let or const inside a code block, it will not be available outside of the block. However, variables outside the block will be available inside the block.

If a variable with the same name as one pre-existing in the global scope is created inside a code block and the variable is defined with let or const, then it will be completely separate. If it is defined with var, then it will override the global variable.

When should you define a new variable?

One variable should be used to represent a single concept. It is important to be careful not store multiple values within one identifier. For example, to parse a query string parameter value, it is best to create one identifier for the URL, one for the query string, and another for the parameter value.

...

Resources for Further Exploration:

JavaScript ES6+: var, let, or const?

Top comments (0)