Sitemap

Variable Varieties in JavaScript

3 min readJan 15, 2023

--

A JavaScript variable is simply the name of the storage location. There are two types of variables in JavaScript: local variable and global variable. There are some rules while declaring a JavaScript variable (also known as identifiers). The name must start with a letter (a to z or A to Z), underscore (_), or dollar ($) sign.

Always declare JavaScript variables with var, let, or const. The var keyword is used in all JavaScript code from 1995 to 2015. The let and const keywords were added to JavaScript in 2015. If you want your code to run in older browsers, you must use var.

4 Ways to Declare a JavaScript Variable:

· Using var

· Using let

· Using const

· Using nothing

var x = 5;

var y = 6;

var z = x + y;

or,

let x = 5;

let y = 6;

let z = x + y;

or,

x = 5;

y = 6;

z = x + y;

From all the examples above, you can guess:

x stores the value 5

y stores the value 6

z stores the value (5+6)=11

When to Use JavaScript const?

If you want a general rule: always declare variables with const. But, if you think the value of the variable can change, use let. In this example, price1, price2, and total, are variables:

Example

const price1 = 5;

const price2 = 6;

let total = price1 + price2;

Get Abdullah Al Mizan’s stories in your inbox

Join Medium for free to get updates from this writer.

All JavaScript variables must be identified with unique names. These unique names are called identifiers. Identifiers can be short names (like x and y) or more descriptive names (age, sum, totalVolume).

The general rules for constructing names for variables (unique identifiers) are:

· Names can contain letters, digits, underscores, and dollar signs.

· Names must begin with a letter.

· Names can also begin with $ and _ (but we will not use it in this tutorial).

· Names are case sensitive (y and Y are different variables).

· Reserved words (like JavaScript keywords) cannot be used as names.

JavaScript Data Types:

JavaScript variables can hold numbers like 100 and text values like “John Doe”.In programming, text values are called text strings.

JavaScript can handle many types of data, but for now, just think of numbers and strings.

Strings are written inside double or single quotes. Numbers are written without quotes.

If you put a number in quotes, it will be treated as a text string.

carName = “Volvo” or let carName = “Volvo”;

In the example above, we create a variable called carName and assign the value “Volvo” to it.

One Statement, Many Variables:

You can declare many variables in one statement. Start the statement with let and separate the variables by a comma:

let person = “John Doe”, carName = “Volvo”, price = 200;

undefined Value:

A variable declared without a value will have the value undefined.

Re-Declaring JavaScript Variables:

If you re-declare a JavaScript variable declared with var, it will not lose its value.

The variable carName will still have the value “Volvo” after the execution of these statements:

Example

var carName = “Volvo”;

var carName;

You cannot re-declare a variable declared with let or const.

This will not work:

let carName = “Volvo”;

let carName;

--

--