Author: M Abo Bakar Aslam

Variables and their Declaration

Variables are containers for storing values. JavaScript Variables can be declared in 4 ways

  1. Automatically
  2. Using var
  3. Using let
  4. Using const

1. Automatically Variable Declaration

Example Code 1: Variables are declared automatically without any data type.

//variable declaration and initialization
x = 5; //integral type variable
z = 5.5; //float type variable
w = "Web Development" //string type variable
t = 'Web Development' //string initialize with single quotation marks
a = "123" //it is string instead of number
  
//displaying values of above variables
console.log("Value of x")
console.log(x)
console.log("Value of z")
console.log(z)
console.log("Value of w")
console.log(w)
console.log("Value of t")
console.log(t)
console.log("Value of a")
console.log(a)

2. Using var

The var keyword was used in all JavaScript code from 1995 to 2015. It should only be used in code written for older browsers. In other words, only use var if you MUST support old browsers.

Example 2:

//variable declaration and initialization
var x = 5; //integral type variable
var z = 5.5; //float type variable
var w = "Web Development" //string type variable
var t = 'Web Development' //string initialize with single quotation marks
var a = "123" //it is string instead of number
  
//Updating value of w
w = 12 
  
//displaying values of above variables
console.log("Value of x")
console.log(x)
console.log("Value of z")
console.log(z)
console.log("Value of w")
console.log(w)
console.log("Value of t")
console.log(t)
console.log("Value of a")
console.log(a)

3. Using const

The const keywords is added to JavaScript in 2015. Always use const if the value should not be changed. Always use const if the type should not be changed (Arrays and Objects)

Example 3:

//variable declaration and initialization
const x = 5; //integral type variable
const z = 5.5; //float type variable
const w = "Web Development" //string type variable
const t = 'Web Development' //string initialize with single quotation marks
const a = "123" //it is string instead of number
  
//Error Line: cannot be updated
t = 12 
  
//displaying values of above variables
console.log("Value of x")
console.log(x)
console.log("Value of z")
console.log(z)
console.log("Value of w")
console.log(w)
console.log("Value of t")
console.log(t)
console.log("Value of a")
console.log(a)

4. Using let

The let keywords is added to JavaScript in 2015 along with const. Always use let if the value can be changed.

Example 4:

Note: JavaScript identifiers are case-sensitive

    //variable declaration and initialization
let x = 5; //integral type variable
let z = 5.5; //float type variable
let w = "Web Development" //string type variable
let t = 'Web Development' //string initialize with single quotation marks
let a = "123" //it is string instead of number
  
//Updating value of w
w = 12 
  
//displaying values of above variables
console.log("Value of x")
console.log(x)
console.log("Value of z")
console.log(z)
console.log("Value of w")
console.log(w)
console.log("Value of t")
console.log(t)
console.log("Value of a")
console.log(a)