HOLIDAY SALE! Save 50% on Membership with code HOLIDAY50. Save 15% on Mentorship with code HOLIDAY15.

3) Learn to Crawl Before You Can Run Lesson

Variables in JavaScript: Understanding What They Are

8 min to complete · By Ian Currie

In this lesson you will deal with variables which are fundamental building blocks of any programming language.

What Are Variables

Variables are a way to store and manage information. This is a common concept used across programming languages, and each programming language has their own ways of dealing with variables.

Variables act as containers used to store and manipulate data. They have names, and they function to hold important values for future use and manipulation.

How to Create a JavaScript Variable

There are three stages: declaration, initialization and assignment

Declaration

Declaring a variable involves using a keyword (such as let or const) followed by a word that will act as the variable's name. In the example below, the keyword is let and the name is age.

Colorful illustration of a light bulb

Don't worry if you don't know what keywords are. You will learn about them at the end of the lesson.

let age;

Initialization

Initializing involves assigning an initial value to a declared variable. In the example below, the initial value is 20.

let age = 20;

Assignment

Assignment is the process of assigning a new value to an already declared and initialized variable.

age = 30;
Colorful illustration of a light bulb

The single equals sign =, is commonly used as an assignment statement. The value on the right side of the equal sign is what is being assigned to the entity that is on the left side. Usually, declaration, initialization and assignment are all done on one line, as seen in the examples above.

Colorful illustration of a light bulb

JavaScript variables must be declared before the variable can be assigned a value. You can also declare a variable and not assign anything to it.

What Are the Different Types of Variables

There are two main types of variable declaration let and const.

Define Variables With: let

Variables with values that can be changed are declared with the let keyword. This will be the way most of your variables will be declared. In fact, just declare all your variables with let, for this course you won't need anything else!

This naming convention was borrowed from mathematics, especially proof writing, where it is used to indicate a variable exists only for the scope of that logical idea. The scope of variables will be discussed later, though you can think of it as its 'area of influence'. You can call a variable within a certain area of your program, but outside that area, you can try calling it, but it won't answer.

let me = "learner";

Define Variables With: const

Variables with values that cannot be reassigned are declared with the const keyword. If you try to even assign the same value it has, it will give you a TypeError on the console. This can be used to make sure you don't change it by accident. Maybe you want to store a constant value like pi.

const pi = 3.141592653589793238;

That said, it's not completely constant, because you can mutate objects and arrays - but more of that later in the course.

Colorful illustration of a light bulb

You may have noticed that when on the console, when you declare a variable, the console returns undefined - you can safely ignore that for now. It doesn't mean that your variable is undefined. It has to do with the fact that the let or const keywords are their own processes, and they themselves do not return any value, they just create a variable and stop. When they stop, the interpreter looks for what the process returns, and since there is nothing, it says undefined.

The Old Way to Define a Variable: var

var brain = "absorber of information";

var is another way to declare variables with changeable values. Before ECMAScript 6 (2015), var was the standard. Despite being superseded by let and const, var is not completeley obsolete; it behaves differently, particularly with scope rules. It's less commonly used in modern code but is still present in legacy code and has unique behaviors such as hoisting, which can be useful in certain contexts.

Why has varnot been removed? Because there are likely millions of scripts everywhere on the web, many of which use var. To deprecate it would literally break the internet! For more information about the difference between let and var, check out this great Stack Overflow answer.

Summary: What As a JavaScript Variable

  • Variables are a way to store and manipulate data
  • They have three components: a keyword, a name and a value

How to Create a Variable

  1. Declaration: choose a keyword and a name
  2. Initialization: give your variable an initial value
  3. Assignment: change your variables value

What Are the Different Types of Variables

  • let is used when you want your variable's value to be able to change
  • const is used when you want to the value of your variable to remain the same
  • var is the old way to declaring variables and should not be used in this course