Var, Let, and Const in JavaScript

Β·

1 min read

Types of variables

  • Var --> function scoped
  • Let --> block scoped
  • Const --> block scoped

Var

When a Var variable is declared outside the function , it becomes globally scoped .

If they are inside a function we only can access them inside the function.

goodbye variable isfunction-scoped as it's limited to that function only. An example of the same is given belowπŸ‘‡πŸ‘‡πŸ‘‡ carbon (2).png One of the issues with using the var keyword is they can be redeclared inside the same scope. carbon (3).png

Let

let variables are block-scoped.also , they can not be redaclared. Like var, a let variable and its value can be updated in the same scope. but, we can’t redeclare a let variable.

It is generally used inside the loops & conditional blocks .πŸ‘‡πŸ‘‡πŸ‘‡ 1_U6RyPv4PvUke9wjK_LH6Kw (1).png

Const

The variables associated with the const are the block scoped variable.

They can be only accessed within the block of code.

Const variables cannot be updated or redeclared. πŸ‘‡πŸ‘‡πŸ‘‡ 1_Kedw3PQdq1ZsseTEiRkc9w.png

Connect With Me!

Twitter: twitter.com/omkarBorude

LinkedIn: linkedin.com/in/omkar-borude-b4583016b

GitHub: github.com/omkarborude

Β