Skip to main content

Command Palette

Search for a command to run...

Variables in JavaScript

Published
•3 min read•View as Markdown
Variables in JavaScript
N

Aspiring Full stack developer

There are 2 way to declare variables in JavaScript:

  1. with let, var and const keywords.

  2. without any keywords.

With let, var and const

let and const are the keywords introduced in ES6. whereas var was introduced in ES5.

var fName = 'nithin';
const age = 23;
let isMarried = false;

without any kewords

But please note that, it is not recommended to use this declaration, as it does create any scope in memory.

isAdult = true;

Difference between let, var and const

  1. Initializing a value during declaration

    With let and var, we can declare a variable without assigning a value to it. but with const we must assign a value to it while declaring a variable. otherwise, you will get a error message saying “SyntaxError: Missing initializer in const declaration“. for ex:

     let age;
     var fName;
     //for const you will get an error msg as "SyntaxError: Missing initializer in const declaration"
     const arr;
    
     //To resolve above issue, initialize it with any value.
     const arr = []
    
  2. Scope of the variables

    Scope of let and const are block scoped. but var is function scoped. means that let and const variable has scope within the blocks only. but var has scope within the entire function. for ex:

function scopeVisualizer() {
// here let,const and var variables will be available to the entire function. since entire function itself is a block of code.
 let functionScopeLet = 'im function scope since i am declared at the top of the function';
 const functionScopeConst = 'im function scope since im declared at the top of the function';
 var functionScopeVar = 'im function scope since im declared at the top of the function';

// inside this if statement let and const will be block scoped. but var is function scoped.
if(true) {
 let blockScopeLet = 'im block scope, i am only available inside this if block only';
 const blockScopeConst = 'im block scope, i am available inside this if block only';
 var functionScopeVar = 'im function scope, even after im declared inside if block, i am available within the entire function';
}

// below will give the result or output since all these variables are at the function scope
console.log(functionScopeLet)
console.log(functionScopeConst)
console.log(functionScopeVar)

//from below let and const variable will throw err. since its block scoped. but the one with var will give proper result.

console.log(functionScopeVar);
console.log(blockScopeLet);
console.log(blockScopeConst);
}

3. Hoisting

All the variables are hoisted. but for let and const they will be in temporal dead zone

// this will result in undefined.
console.log(age);
var age;

// let and const will be hoisted. but will be in temporal dead zone
console.log(fName)
let fName;

console.log(age)
const age = 23;
  1. Reassigning values

    with var and let variables, we can reassign the values. but with const we cannot reassign the value once it has been declared.

var age = 23;
age = 30;

let fName = 'Nithin';
fName = 'Virat';

// This will throw an error. "Reassinging values to const variable"
const lName = 'Kumar';
lName = 'Kohli'