2

I saw an interview question online:

function b() {
    b = 5
    console.log(b) // 5
}
b()
console.log(b) // 5

var a = 20
var c = (function a() {
    a = 10
    console.log(a) // f a() { a = 10; console.log(a) }
})()
console.log(a) // 20

Why does console.log(a) print function a() { a = 10; console.log(a) } instead of 10?

Not A Bot
  • 2,474
  • 2
  • 16
  • 33
  • 5
    The older I get the less useful I find asking these sort of questions. I'd much rather walk through some code that the candidate is especially proud of and discuss why they have done things in a certain way. If code such as the above were in production, it would be backed up by a comment. – Bathsheba Jan 20 '21 at 08:11
  • 3
    Because https://tc39.es/ecma262/2020/#sec-function-definitions-runtime-semantics-evaluation TLDR in function expression binding to name `a` is immutable. – Yury Tarabanko Jan 20 '21 at 08:24

0 Answers0