Skip to content

Variables

In Schools, we all have encountered those maths problems where we were given few letters with some value assigned to it. Let me jog your memory using a very simple math problem as shown below.

let value of x be 3 and y be 4, then what will be x + y ?

I know it’s a very basic problem. Right? It was pretty easy and our mind was trained to see these X and Y letters as numbers. So if we saw these problems, our minds immediately replaced x + y with 3 + 4 and we got the answer 7.

But did you observe something here? While problem literally statement said “add X with Y”, but we never got confused with the letters! We immediately replaced letters with their respective numeric values and performed the arithmetic problem.

Think of variables exactly the same way you thought of the letters in the above math problem. When you saw x, you immediately knew that it meant 3 and same for y respectively.

A variable is like a container that holds a particular value on which a certain operation will be performed in the future. As a box contains some stuff, a variable reserves some space in the memory of your computer which may contain some value.

In JavaScript, there are three ways to declare a variable but first let’s look at the syntax.

 variable_keyword  variable_name = some_value

In above syntax:

  1. variable_keyword refers to var, const and let.
  2. variable_name refers to user defined name for the variable.
  3. some_value refers to the value that variable contains.

Here’s an example:

const username = "thatsmanmeet";

In above example:

  1. const is variable_keyword.
  2. username is variable_name.
  3. thatsmanmeet is some_value.
  1. const: It is used to define a variable whose value never changes.
  2. let: It is used to define a variable whose value can be changed over time.
  3. var: It works just like let but is an old way in JS.

Example:

// Declaring variables in JavaScript:
const birthYear = 2002;
let age = 23;
var weight = "75Kg";

// Changing a let variable:
age = age + 1; // now age is 24

// Attempting to change a const:
birthYear = 2003; // ❌ TypeError: Assignment to constant variable.

In above code example, const is used to define the birthYear as 2002 as the birth year of a person will remain constant and will not be changed. Now the age is 23, but it will be changed next year to 24, so let will be appropriate here. In case of var, I have used weight, which can also change over time. Hence this shows how these variables can be used in context of their immutability.

We can also see how assigning the value again to birthYear will cause Error in our code and will crash our code. We will learn about errors in the Error Handling article.

Variables are the building blocks of every program: they let us store, update, and work with data as our code runs. You’ve now seen:

  1. What a variable is – a named container for a value
  2. How to declare one – using const, let, or var
  3. When to pick each keyword – immutability with const, re-assignable with let, and the legacy behavior of var

So that’s all about introduction to the variables. let’s continue to next section to learn more about the variables.