During programming variables are used to store the data and we declare different type of variables to store different type of data in them. JavaScript Variables can be declared in 4 ways:

  • Automatically
  • Using var
  • Using let
  • Using const

Automatic Declaration:

Let’s check an example below. In below example, x, y, and z are undeclared variables. They will get automatically declared as per the data when used first time. But it is considered a good programming practice to always declare variables before we use.

x = 4;
y = 6;
z = x + y;

Using Var

The ‘var‘ keyword was used in all JavaScript code from 1995 to 2015 and even till date we are using this. In below example x stores 4 and y stores 6 and next z stores as 10 in it

var x = 4;
var y = 6;
var z = x + y;

Using let

For old browser we can use ‘var’ but for modern browsers we use ‘let’ and ‘const’ keywords and these were added to JavaScript in 2015.

Note: only let will work to you. One cannot use LET or Let.

let x = 4;
let y = 6;
let z = x + y;

Using const

This is one another way to declare the variables in Java Scripts and ‘const‘ keywords was added to JavaScript in 2015. Hence for older browser you cannot use this.

const x = 4;
const y = 6;
const z = x + y;

All above variable are declared as constant values and cannot be changed later in you Java script program. So, if you want to change it how we can do this, Let’s check below.

const x = 4;
const y = 6;
let z = x + y;

In above example we cannot change value of ‘x’ and ‘y’ but ‘z’ is declared as let so it can be changes later in your Java Script program.

Many Variables with one statement

You can declare many variables with in one statement. Start the statement with ‘let’ and separate the variables by comma. Let’s check below example.

let employee = "John Smith", carName = "FORD", price = 500;

//above can be also written as below

let person = "John Smith",
carName = "FORD",
price = 500;

The general rules for constructing variables name.

Below are few rules which one must follow while declaring variables.

  • Names can contain letters, digits, underscores, and dollar signs.
  • Names must begin with a letter.
  • Names can also begin with $ and _ (but not suggested).
  • Names are case sensitive (y and Y are different variables in Java script).
  • Reserved words (like JavaScript keywords) cannot be used as names.
  • JavaScript identifiers are case-sensitive.

Important point to remember.

  • In JavaScript, the equal sign (=) is an “assignment” operator, not an “equal to” operator.
  • The “equal to” operator is written like == in JavaScript.
  • 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.
  • It is good programming practice to declare all variables at the beginning of a script.

Explore More

How to remove JavaScript Objects from memory

To remove unused objects and improve memory management in JavaScript, you generally rely on the JavaScript engine’s garbage collection mechanism. However, you can aid this process by explicitly removing references

On click of parent page pop up window should stay on top only

To ensure the popup window stays on top of the parent page, you can use the window.open() method with specific focus-handling logic. JavaScript allows you to bring the popup window

Open a Fixed-Size Popup Without Minimize/Maximize Effects

Disabling the minimize and maximize buttons of a popup window is not directly supported in modern browsers, including Microsoft Edge, due to security and user-experience considerations. The window.open() function provides