TExcelTAD
Home › JavaScript › JS Variables

JavaScript Variables

A variable is a named container for a value. You give it a name once, and from then on you can read the value, pass it around, and (for some kinds of variables) replace it with a new value.

Declaring variables

Modern JavaScript has three keywords to declare a variable:

Keyword Can be reassigned? Scope When to use
const No Block The default choice for most values
let Yes Block Counters and values that really change
var Yes Function Old code only — avoid in new code
js · try it yourself
const city = "Lisbon";      // will not be reassigned
let visitors = 120;         // will change
visitors = visitors + 30;

console.log(city, visitors);
Lisbon 150

The = sign is the assignment operator: it stores the value on the right in the variable on the left. It does not mean "equals" in the mathematical sense — visitors = visitors + 30 reads as "take the current value, add 30, store the result back".

Declaring first, assigning later

A variable declared with let but not given a value holds the special value undefined until you assign one:

js · try it yourself
let result;
console.log(result);
result = 7 * 6;
console.log(result);
undefined
42

You can declare several variables in one statement, separated by commas — though one declaration per line is usually easier to read:

js · try it yourself
let width = 4, height = 3, depth = 2;
console.log(width * height * depth);
24

Variables hold any type

A variable has no fixed type. The same let variable could hold a number, then a string, then an array (although doing that on purpose makes code harder to follow):

js · try it yourself
let item = 42;
console.log(item, typeof item);
item = "forty-two";
console.log(item, typeof item);
item = [4, 2];
console.log(item, typeof item);
42 number
forty-two string
[ 4, 2 ] object

Naming variables

Good names make code self-explanatory. Follow the identifier rules from the Syntax chapter (letters, digits, _, $, no leading digit, no keywords) and these habits:

  • Use camelCase: firstName, cartTotal, isLoggedIn.
  • Be descriptive: price beats p, userCount beats n.
  • Booleans read well as questions: isOpen, hasError, canEdit.
  • Short names like i or x are fine for tiny scopes such as loop counters.

Using variables in expressions

Variables can be combined with operators just like literal values. With strings, + joins them together:

js · try it yourself
const firstName = "Grace";
const lastName = "Hopper";
const fullName = firstName + " " + lastName;
const birthYear = 1906;
const ageIn1950 = 1950 - birthYear;

console.log(fullName);
console.log(fullName + " was " + ageIn1950 + " in 1950.");
Grace Hopper
Grace Hopper was 44 in 1950.

What about var?

Before 2015, var was the only way to declare a variable. It still works, but it has confusing rules: it ignores block boundaries, it can be redeclared silently, and it is hoisted in a way that lets you use it before its declaration. The next two chapters show how let and const fix these problems.

js
var total = 10;
var total = 20;   // no error — easy to clobber a variable by accident

Tip: Start every variable as const. Change it to let only when you find you actually need to reassign it.

Test yourself

What is the value of a variable declared with let x; before anything is assigned?

  1. 0
  2. null
  3. undefined
  4. ""

Which keyword should be your default for new variables?

  1. var
  2. let
  3. const
  4. static

After let n = 5; n = n * 2;, what is n?

  1. 5
  2. 10
  3. 25
  4. An error