Are you sure? You know javascript, well answers these snippets

Q1. Guess the output?

var age = 21;
var myAge = function () {
console.log(age);
var age = 20;
};
myAge ();

Wait Think Hard, Don’t Jump into a solution

Ouput: undefined

Reason: the reason is that when the function is executed, it checks that there’s a local age variable present but doesn’t yet declare it so that it won’t look for a global one

Q2. Guess the output?

var getValue= [undefined];
getValue[2] = 1;
console.log(getValue);
console.log(getValue.map(e => 7));

Wait Think Hard, Don’t Jump into a solution

Output:[ undefined, <1 empty item>, 1 ]
[ 7, <1 empty item>, 7 ]

Reason: It will not crash. The JavaScript engine will make empty array slots if there is no value

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Vineet Mishra

Vineet Mishra

Full Stack Developer at CCR Tech, I write about Mobile/Web Development and Deployment, and Interview Q & A