Variables and Values

Variables are assigned to values. After variables are assigned, they can be reassigned to other values.

var world; // the variable world is declared
world = "pretty"; // world is assigned to the value "pretty"
world = 20; // world is reassigned to the value 20

Different variables can be assigned to the same value;

var world = "awesome";
var waterfall = "awesome";
world === waterfall // true

You can assign variables to any type of value. Numbers (45), strings ("something"), and booleans true are all valid examples.

In the following statement, the variable mood is assigned to the value 'happy':

var mood = 'happy';

In the following statement, the variable bestNumber is assigned to the value 8:

var bestNumber = 8;

In the following statement, the variable isCorrect is assigned to the value false:

var isCorrect = false;
Question Click to View Answer

Identify the variables and values in the following statements:

var hiThere = "greetings";
var numNum = 55;
var liar = false;

hiThere, numNum, and liar are variables. "greetings", 55, and false are values.

Variables are assigned to values.

What does the following code print to the console?

var myIq = 10;
var yourIq = 170;
console.log(myIq > yourIq);
false

The myIq variable is assigned to the value 10. The yourIq variable is assigned to the value 170. The boolean condition (myIq > yourIq) evalues to false.

What does the following code print to the console?

var gg = 4;
var ll = 4;
console.log(gg === ll);
true

The gg and ll variables are both assigned to the value 4. The === operator returns true when both of the operands are the same. In this example, gg and ll are the operands and === is the operator.

<-- Previous (Declaring Variables) Next (Variable Reassignment) -->