DEV Community

Discussion on: JS interview in 2 minutes / var ⚔️ let ⚔️ const

Collapse
 
pelayomendez profile image
Pelayo Méndez

Actually when you do this:
x = 2
You are declaring the variable.

That's why:
x = 2
var y = x + 1
console.log(y) // 3

Collapse
 
pelayomendez profile image
Pelayo Méndez • Edited

Maybe something like this will help to understand better your point:

console.log(x) // undefined
x = 2
var y = x + 1
console.log(y) // 3
var x;

console.log(x) // error
x = 2
var y = x + 1
console.log(y)

I came back over and over to this topic and always find new thigs ;)

Collapse
 
hexnickk profile image
Nick K

I came back over and over to this topic and always find new thigs ;)

yeah, some part of JS is so confusing 🤷 Thanks for a detailed example!