JavaScript Variable

JavaScript Variables

In Javascript there are Four Ways to Declare a JavaScript Variable:

 

Using var Using let Using const Using nothing

Variables are containers for storing data (storing data values).

The var is the oldest keyword to declare a variable in JavaScript.

Scope: Global scoped or function scoped. The scope of the var keyword is the global or function scope. It means variables defined outside the function can be accessed globally, and variables defined inside a particular function can be accessed within the function.

var x = 5;
var y = 6;
var z = x + y;
console.log("The value of z is: " + z);
// Result: The value of z is: 11

In this example, xy, and z, are variables, declared with the var keyword:

The let keyword is an improved version of the var keyword.

Scope: block scoped: The scope of a let variable is only block scoped. It can’t be accessible outside the particular block ({block}). Let’s see the below example.

let x = 5;
let y = 6;
let z = x + y;
console.log("The value of z is: " + z);
// Result: The value of z is: 11

In this example, xy, and z, are variables, declared with the let keyword:

The const keyword has all the properties that are the same as the let keyword, except the user cannot update it.

Scope: block scoped: When users declare a const variable, they need to initialize it, otherwise, it returns an error. The user cannot update the const variable once it is declared.

const price1 = 5;
const price2 = 6;
let total = price1 + price2;
console.log("The total is: " + total);
// Result: The total is: 11

When to Use JavaScript var?

A variable is a “named storage” for data. We can use variables to store goodies, visitors, and other data.

Always declare JavaScript variables with var,let, orconst.

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 browser, you must use var.

Difference between var, let and const keywords in JavaScript

var let const
The scope of a var variable is functional scope. The scope of a let variable is block scope. The scope of a const variable is block scope.
It can be updated and re-declared into the scope. It can be updated but cannot be re-declared into the scope. It cannot be updated or re-declared into the scope.
It can be declared without initialisation. It can be declared without initialisation. It cannot be declared without initialisation.
It can be accessed without initialisation as its default value is “undefined”. It cannot be accessed without initialisation, as it returns an error. It cannot be accessed without initialisation, as it cannot be declared without initialisation.

Note: Sometimes, users face the problem while working with the var variable as they change the value of it in the particular block. So, users should use the let and const keyword to declare a variable in JavaScript.

Summary:

We can declare variables to store data by using the varlet, or const keywords.

  • let – is a modern variable declaration.
  • var – is an old-school variable declaration. Normally we don’t use it at all, but we’ll cover subtle differences from let in the chapter The old “var”, just in case you need them.
  • const – is like let, but the value of the variable can’t be changed.

Variables should be named in a way that allows us to easily understand what’s inside them.

Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *

five + two =

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top