Are You Afraid To Solve These 3 JavaScript Questions?
I was too.

Recently, I switched my job and attended more than 30 interviews. It was a relatively enjoyable experience and I faced all of the questions with ease. However, there were some that were very tricky to answer like these.
1. Accidental Global Variable
function crazyFunction() { var a = (b = 10);}crazyFunction();console.log("b", typeof b === "undefined");console.log("a", typeof a === "undefined");
2. Eye Test
const numbers = [1, 2, 3];for (var index = 0; index < numbers.length; index++);{ const number = numbers[index]; console.log(number);}
3. Tricky Closure
let i;for (i = 0; i < 3; i++) { const log = () => { console.log(i); }; setTimeout(log, 100);}
Are you ready to check the answers? Revealing them here.
1. b false
a true2. undefined3. 3
3
3
I hope you were able to find the answers to these questions.
You can checkout more questions here
Do you want a summary of my reading? Follow me on Twitter, where I post highlights from books and articles!
Cheers!
More content at PlainEnglish.io. Sign up for our free weekly newsletter. Follow us on Twitter and LinkedIn. Join our community Discord.