Enea Xharja

Software Engineer

Values and Variables in JavaScript

Primitive Values Are Immutable

Primitive values are immutable. They are the building blocks of JavaScript and cannot be created, destroyed, or modified.

Consider a string and an array. A string is a sequence of characters, while an array is a sequence of items:

let str = "hello";
let arr = ["hello", "world"];

We can access the first item of a string and an array in the same way:

console.log(str[0]); // "h"
console.log(arr[0]); // "hello"

Intuitively, it's easy to assume that you can change the first item of a string just as you can change the first item of an array:

str[0] = "j";
arr[0] = "hi";

console.log(str); // "hello"
console.log(arr); // ["hi", "world"]

But it is not possible. A string is a primitive value, and all primitive values are immutable or read-only. An array, on the other hand, is not a primitive value, so we can modify its items.

Variables Are Wires

Consider this example:

let greeting = "hello";
greeting = "hi";
console.log(greeting); // "hi"

We just mentioned that strings are immutable or read-only. So why does the variable greeting change to "hi"?

It is important to understand that a variable is not a value. A variable is like a wire that points to a particular value. We can change the value it points to by using the assignment operator =.

When you write: let greeting = "hello"; we create a variable called greeting and point it to the value "hello". Then, when we write greeting = "hi"; we are changing the value to which the variable greeting points to.

When you want to use the assignment operator =, there are two rules to follow:

  • the left side of the assignment must be a variable (or wire)
  • the right side of the assignment must be an expression (or question you ask to JavaScript) that results in a value.

Note that when you write let age = 42;, the value 42 is also an expression. It is an expression that results in the value 42 and is called a literal value, because we literally write the value that the expression results in.

Also, it is not possible to point variables to each other. Variables always point to values. When you see an assignment, you ask for the right-side value and point the wire on the left-side to that value.

Reading the Value of a Variable

Reading the value of a variable is as simple as logging it into the console:

let pet = "dog";
console.log(pet); // "dog"

It is not the variable pet that is passed to console.log. You cannot pass variables to functions. You pass the current value of the variable pet.

When you write pet, you ask JavaScript the question: "What is the current value of pet?". To answer the question, JavaScript follows the pet wire and returns us the value at the end of this pet wire.

Here is another example:

function double(x) {
  return (x = x * 2);
}

let money = 10;
console.log(double(money)); // 20
console.log(money); // 10

Here: double(money) means that you first determine the value of money and then pass that value to double. So money still points to the value 10.