Basic Variables

Variables are used to store information to be referenced and manipulated in a computer program. They also provide a way of labeling data with a descriptive name, so your programs can be understood more clearly by programmers. It is helpful to think of variables as containers that hold information. Their sole purpose is to label and store data in computer memory. This data can then be used and even changed throughout the lifetime of your program.

When you finish this reading, you should be able to:

Initializing a variable

To initialize a variable in JavaScript, you'll need two new pieces of syntax: let and =. You can give the variable any name that you wish and assign it a value. Once you initialize a variable, the variable will evaluate to the value assigned:

let bootcamp = "App Academy";
console.log(bootcamp);  // 'App Academy'

let birthYear = 2012;
console.log(birthYear); // 2012
    

Did you know? JavaScript variables names can contain any alphanumeric characters, underscore (_), or dollar sign ($). However, they cannot begin with a number.

Above are examples of how you'll create variables most of the time. As a best practice, you should name your variables in a way that is descriptive and concise.

The variable initializations above really consist of two steps: declaration with let and assignment with =. Let's break these two steps down.

Declaring a variable

In JavaScript, in order to use a variable, you must declare it. Variable declaration is the act of introducing the variable to the environment.

To declare a variable, use the let keyword, followed by a space and then the name of the variable.

let bootcamp;
console.log(bootcamp); // undefined
    

Once a variable is declared, it will contain undefined as its value. undefined is a common default value in JavaScript, and you'll see it come up in a few different places. You can think of undefined as showing that the variable is empty.

Assigning a variable

Once a variable has been declared, you can assign it a value using single-equals = :

let bootcamp;
console.log(bootcamp); // undefined
bootcamp = "App Academy";
console.log(bootcamp); // 'App Academy'
    

Manipulating variables

To change the value of a variable, you need to reassign it to a new value with = :

let num = 42;
console.log(num + 8); // => 50
console.log(num); // => 42

num = num + 10;
console.log(num); // => 52
    

In the code above, num + 8 will evaluate to 50, but it will not change the num variable to 50. If you want to change the num variable, you must reassign to it.

Assignment Shorthand

Changing the value of a number variable is something fairly common in the programming world. Luckily there are some shorthand operators you can use:

let number = 0;
number += 10; // equivalent to number = number + 10 //=> 10
number -= 2;  // equivalent to number = number - 2  //=> 8
number /= 4;  // equivalent to number = number / 4  //=> 2
number *= 7;  // equivalent to number = number * 7  //=> 14
console.log(number); // 14
    

There are also other shorthand operators to add or subtract exactly 1 from a variable, the increment (++) and decrement (--) operators:

let year = 3004;
year++;
console.log(year); // 3005
year--;
console.log(year); // 3004
    

NaN

Now that you have the ability to perform arithmetic with variables, let's take a look at a common programming mistake, getting a result of NaN (not a number):

let num;
console.log(num + 3); // NaN
    

The above code gives NaN because the unassigned num variable contains undefined; adding 3 to undefined results in NaN. In general, any nonsensical arithmetic will result in NaN. Math operations involving undefined is perhaps the most common mistake:

console.log(undefined + 3); // NaN
console.log("fish" * 2); // NaN
    

What you've learned