Variables are named memory locations in JavaScript. These memory locations can contain values of any type recognized by JavaScript. Such a named memory location is created by declaring a variable. In JavaScript var, let and const are used to declare a variable.

var a;
let b;
const c = 3; // Assignment is mandatory for const

Default Value

If we declare a variable using var or let and do not assign any value to it, the default value is undefined.

var a;
console.log(a); // undefined

let b;
console.log(b); // undefined

So, if we need to check if a variable is not initialized, we can do following conditional check.

var a;
if (a !== undefined) {
  console.log("a is initialized");

In case of const, it is mandatory to assign a value at the time of declaration. Otherwise it will throw an error.

const a; // SyntaxError: Missing initializer in const declaration

Variables are untyped

A variable declared in JavaScript does not hold any particular type. We can assign value of any type to a variable and later it can be reassigned with a variable of another type.

var a = "Hello" // Type of `a` is string
a = 10 // Type of `a` is not number

let b = true // Type of `b` is boolean
b = null // Type of `b` is now null

A variable declared using const is meant to be a constant. So, reassigning with another value throws an error.

const a = 5;
a = "Hi"; // TypeError: Assignment to constant variable.