Values And Variables

Introduction to JavaScript values and variables and how we use them.


In JavaScript, all data or chunks of data can be called values. Values come in different shapes and sizes, some mutable and others not. While the majority of values are an object type, JavaScript comes with other types of values as well.


Variables allow users to point to and refer to values in the JavaScript data-space. All values that are initialized or simply exist in JavaScript are accessed through variables. Let’s call them pathways to values. See how we can declare a variable.

var variable; // declare variable named "variable"
console.log(variable);// print variable

A variable is declared with the reserved keyword var. Initially, all variables, when declared, point to no value and so JavaScript will assign undefined to them. To make pathways to different values, we either assign them to the values at initialization or later in the code.

Check out how an assignment changes the look of a variable.

Assignment of variables

To use value through a variable, we need to assign the variable to the data using the = operator.

NOTE: It doesn’t really matter what you name the variable as long as it is not the same as any JavaScript reserved keywords.

See what happens when a variable is assigned a value.

We can see above that initially, the value and variable are two disconnected components where the value is inaccessible to us. To use it everywhere or to not lose the value, we link it to a variable. The variable, linked through the assignment operator =, keeps track of the value which in this case is 5000.

Examine the assignment operator in action below.

var variable_1; // variable declaration
variable_1 = 25; // variable assignment
console.log("variable_1:",variable_1); // variable printing
var variable_2 = 30; // variable declaration and assignment
console.log("variable_2:",variable_2); // variable printing

In the code above we see how values are stored in variable_1 and variable_2. Using the = operator, we assign the values 25 and 30 respectively to the two variables. Observe that the assignment operator can be used at the point of declaration or after it.

variable_1 = 10; // declaration and assignment without 'var'

Another way of initializing a variable is to directly assign a value to it without using the var keyword. This approach works, but is depreciated as it will declare the variable as a global variable.

var variable_1 = 1; // initialised using var to 1
let variable_2 = 2; // initialised using let to 2
const variable_3 = 3; // initialised using const to 3
variable_4 = 4; // initialised directly to 4
console.log(variable_1); // printing variable_1
console.log(variable_2); // printing variable_2
console.log(variable_3); // printing variable_3
console.log(variable_4); // printing variable_4

You can also declare variables by using let and const. These approaches function like var. However, JavaScript interprets and defines their scope very differently.