DEV Community

Cover image for Var vs Const vs Let (JavaScript)
Hardik Mirg
Hardik Mirg

Posted on • Updated on

Var vs Const vs Let (JavaScript)

Variable Declarations

There are several ways of declaring values to variables in javascript:

  • Var
  • Const
  • Let

Var

var stands for "variable" is used to declare variables that can be reassigned and are only available inside the function they're created in. They're function scoped.

var word = "hello"
console.log(word) // returns "hello"

word = "bye" // can be re-assigned ✅
console.log(word) // returns "bye"
Enter fullscreen mode Exit fullscreen mode

Const

const stands for "constant" and is used to declare variables that cannot be reassigned and are not accessible before they appear within the code. They're block scoped.

const word = "hello"
console.log(word) // returns "hello"

word = "bye" // cannot be re-assigned ❌
console.log(word) // throws an error as constants cannot be re-assigned
Enter fullscreen mode Exit fullscreen mode

Let

Variables declared using let can be reassigned but are similar to const i.e. block scoped. If variables are not created inside a function or block they are globally scoped.

  • Block

    A block is a set of opening and closing curly brackets.

Hope you learned something useful today! Peace Out ✌️

Top comments (0)