Briefly, what is the difference between "var" and "let" keywords in Javascript?

cyebukayire

Peace

Posted on January 19, 2022

Briefly, what is the difference between "var" and "let" keywords in Javascript?

var and let are both used for variable declaration in javascript but the difference between them is that a 'var' variable can be re-initialized while a 'let' variable can only be initialized once.

Example:

var name = "Peter"
var name = "Peace"
console.log(name)
Enter fullscreen mode Exit fullscreen mode

Output
Peace

A keyword called let was introduced in ES6, a major update to JavaScript, to solve this potential issue with the var keyword.
Once a variable has been initialized, you can not re-initialize it.

Example:

let name = "Keza"
let name = "Gisa"
console.log(name)
Enter fullscreen mode Exit fullscreen mode

Output
SyntaxError: unknown: Identifier 'name' has already been declared.

💖 💪 🙅 🚩
cyebukayire
Peace

Posted on January 19, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related