Backbencher.dev

JS Daily 1 - Variable Redeclaration

Last updated on 21 Nov, 2020

What is the output of following code?

var a = 6;
let a = 8;
console.log(a);
----o----

let was introduced in ES6 and is used to declare variables in JavaScript. If a variable with the same name is already declared in the same scope, let throws an error. Here second line throws below error:

Uncaught SyntaxError: Identifier 'a' has already been declared

This behaviour is explained here in ECMAScript.

--- ○ ---
Joby Joseph
Web Architect