JavaScript Interview Questions, Functions and Prototypes
10 min readOct 6, 2018
Below are some interview questions with answers to accompany the other two articles that I wrote on JavaScript functions and prototypes. The questions are grouped in three categories:
- functions
- closures
- prototypes
The answer for each question is given right after the question.
Functions
Question
What is the output of the following? And do you agree with what JavaScript outputs?
const fn = () => {};
console.log(typeof fn);
Answer:
The code above outputs function
. Even though the output is helpful it's not accurate. It would be more accurate to report object
because functions are objects in JavaScript.
Question
What will be the value of this
in the snippet below when the getId
function is called? And what will be the output to the console?
'use strict';
const user = {
id: 551,
name: 'Tom',
getId() {
return this.id;
},
credentials: {
id: 120,
username: 'tom',
getId() {
return this.id;
}
},
};
const getId = user.credentials.getId;
console.log(getId());