DEV Community

Peace
Peace

Posted on

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.

Top comments (1)

Collapse
 
jafuentest profile image
Juan A. Fuentest Torcat • Edited

That is just one of many differences, here's a a more detailed explanation: stackoverflow.com/questions/762011...

But the real point behind let was to change the scope