JavaScript Output Interview Questions - this, Prototypes and Type Coercion (Set 3)

JavaScript Output Interview Questions - this, Prototypes and Type Coercion (Set 3) - CodingHumans

You will see questions like these in almost every frontend interview round. They check how JavaScript decides what this is, how bind and arrow functions change it, how the prototype chain works, and the type conversion rules behind == and the + operator.

How to practise: read each question and tap the option you think is correct. You will see right away whether you got it, along with a short explanation, and your score is tracked at the bottom of the screen. This set has 16 questions.

JavaScript MCQ series: Set 1 | Set 2 | Set 3 | Set 4

Topics covered in this set

  • How this is determined (method call, plain call, bind, arrow)
  • call, apply and bind
  • Prototype chain and hasOwnProperty
  • == vs === and Object.is
  • Type coercion with + and -
  • Truthy and falsy values

JavaScript MCQs with answers and explanations

Q1. What is printed?

const obj = {
  name: 'A',
  greet() {
    return () => this.name;
  },
};
console.log(obj.greet()());

a) undefined

b) ''

c) TypeError

d) A

Q2. In strict mode, what is printed?

const obj = {
  name: 'A',
  get() {
    return this?.name;
  },
};
const fn = obj.get;
console.log(fn());

a) window

b) A

c) undefined

d) TypeError

Q3. What is printed?

function getX() {
  return this.x;
}
const bound = getX.bind({ x: 1 });
console.log(bound.call({ x: 2 }));

a) 1

b) undefined

c) TypeError

d) 2

Q4. What happens here?

const Person = (name) => {
  this.name = name;
};
const p = new Person('Ravi');

a) SyntaxError

b) Creates { name: 'Ravi' }

c) Returns undefined

d) TypeError: Person is not a constructor

Q5. What is the result of [] + {}?

a) NaN

b) 0

c) '[object Object]'

d) undefined

Q6. What is printed?

console.log('5' - 2, '5' + 2);

a) NaN '52'

b) '3' '52'

c) 3 7

d) 3 '52'

Q7. What is printed?

console.log(null == undefined, null === undefined);

a) false true

b) false false

c) true true

d) true false

Q8. Which expression returns true?

a) NaN === NaN

b) Object.is(NaN, NaN)

c) NaN == NaN

d) [NaN].indexOf(NaN) === 0

Q9. What is typeof null?

a) 'number'

b) 'undefined'

c) 'object'

d) 'null'

Q10. What is printed?

console.log([1, 2, 3] == '1,2,3');

a) true

b) false

c) TypeError

d) undefined

Q11. What is printed?

function Dog() {}
Dog.prototype.bark = () => 'woof';
const d = new Dog();
console.log(d.hasOwnProperty('bark'), 'bark' in d);

a) false true

b) false false

c) true true

d) true false

Q12. For class A {} and const a = new A(), which expression is true?

a) a.prototype === A.prototype

b) A.__proto__ === a

c) a.constructor === Object

d) Object.getPrototypeOf(a) === A.prototype

Q13. What is printed?

console.log(0.1 + 0.2 === 0.3);

a) true

b) TypeError

c) NaN

d) false

Q14. Which of these values is falsy?

a) []

b) 0n

c) '0'

d) ' '

Q15. What is printed?

console.log({ a: 1 } == { a: 1 });

a) false

b) TypeError

c) undefined

d) true

Q16. What is printed?

const a = { n: { v: 1 } };
const b = { ...a };
b.n.v = 2;
console.log(a.n.v);

a) TypeError

b) 2

c) 1

d) undefined

Quick revision notes

  • this is decided by how a function is called: obj.method() sets it to obj, a plain call gives undefined in strict mode, and new creates a fresh object.
  • Arrow functions take this from the surrounding scope and cannot be used with new.
  • bind fixes this for good. call and apply set it for one call only.
  • Prefer ===. Remember null == undefined is true, NaN is never equal to itself, and Object.is handles NaN.
  • + joins strings when either side is a string. -, * and / always convert to numbers.

Frequently asked questions

How do I remember how this works in JavaScript?

Look at how the function is called. With new, this is the new object. With bind, call or apply, it is the object you pass. With obj.fn(), it is obj. A plain call gives undefined (in strict mode), and an arrow function uses the this of the code around it.

Should I ever use == in JavaScript?

Most style guides say use === everywhere. One common exception is value == null, which checks for both null and undefined in one go.

What is the difference between __proto__ and prototype?

prototype is a property on constructor functions and classes. __proto__ is the link from an object to the prototype it inherits from, and it is better to read it with Object.getPrototypeOf.

Comments

Answered 0/0Correct 0