Javascript Variables Explained

By onjsdev

Javascript variables are used to store a wide range of data. Imagine that you are developing an e-commerce site, products, quantities, customers, credit card information are all data on this site. At this point, variables are used to store like these pieces of information in memory.

Declaring Variables

You can think of variables as a container that stores a particular information. To create them in javascript, you can use the var, let, or const keywords followed by a variable name:

var productBrand;
let productQuantity;
const productId;

In this snippet above, we have created three variables ( ie defined or declared them) to store information. Now we can put some data on them to store and access this information later.

var productBrand = "Apple";
let productQuantity = 20;
const productId = "Pro Max 15";

Using Variables

Once we create some variables and put information in then, we can now use them in different parts of our javascript application.

let width = 20;
let height = 20;
let area = width * height;
console.log(area) // 400

In this example, we have three variables; The width and height variables store the side lengths of a square, and the area variable stores the area of the square. This example shows that after you declare some variables, you can use them to create another variable or to get information inside them.

Naming Variables

As you can see, we have given different names to each variable. We have some restrictions in giving these names and best practices without obligation mostly used by the javascript community.

These rules are important to make code more readable and understandable. Here are some rules and best practices for naming variables:


let product1 = "Phone"; ✔
let product-1 = "Phone"; ✘

let 9product = "PC" ✘

let while = 4 ✘
  • Variable names can only contain letters, numbers and underscores
  • Variable names cannot start with a number
  • Reserved keywords cannot be used as variable names (if, for, while)
  • Variable names are case sensitive: myVariable, myvariable and MyVariable are all different variables in JavaScript.

Best Practises

let a = "MacBook"; ✘
let computer = "MacBook"; ✔

let phonenumber = "0234058"; ✘
let phoneNumber = "0234058"; ✔

let pi = 3.14; ✘
const PI = 3.14; ✔
  • Variable names should describe the data they store
  • It is common to use camelCase for variable names (productId, phoneNumber )
  • If a value of a variable will not change throughout the program, it is common to use uppercase letters for its name (PI_NUMBER, MAX_NUMBER)


In this guide we have discussed the variables in javascript. The summary of the guide would be:

  • Variables are used to store data on memory
  • Variables can be declared with three keywords (var, let, const)
  • After creating a variable and putting data into it , it can be used in different sections of the application
  • We have some restrictions that is necessity and best practises to make code more readable for giving name to a variable

Learning requires practice, so you should work on variables locally so that you can gain a more comprehensive understanding of the topic.

Thank you for reading.