Welcome to the javascript learning course

Lets start today's topic - How many ways are there to declare variable in javascript.

  1. var
  2. let
  3. const

Lets explore all ways:

Let's start with **var**. Declare a variable by this syntax

Assign a value to the varibale .

Reassign a value to the variable .
Lets see what happens when we declare a variable and print it's value to console.

undefined is printed on console because we didn't assign any value to the variable 'var a'.

Let's see what happens when we assign a value to the variable.

We can see that 1 is printed and when we reassign the value 3 to a, 3 is printed.

var with same name can be redeclared again it will replace the previously defined value.


Next is let way to declare a variable.

When value is not assigned to let is print's undefined.

Value to b can be reassigned to let.

let with the same name cannot be redeclared again it throw's syntax error.


Let's see const in action.

When we declare variable as const we have to initialise it's value if not initialised it throws error.

Can the const variable be redeclared as in it is in var.

const variable cannot be redeclared as in var

Now let's try to reinitialise const variable.

const variable cannot be reinitialised.

That's all for today.