Common JavaScript Interview Questions and Their Analysis

Object Assignment Chaining

var obj1 = { value: 1 }
var obj2 = obj1
obj1.prop = obj1 = { value: 2 }

console.log(obj1.value, obj2.value) // 2 1
console.log(obj1.prop, obj2.prop) // undefined { value: 2 }

This classic interview question hides subtle details. The core of the third line obj1.prop = obj1 = { value: 2 } tests two concepts:

  1. Operator precedence of property access versus assignment.
  2. Chained assignment behavior.

Property access has a higher priority than assignment, so the property access executes first. Before obj1 = { value: 2 } executes, the original object referenced by obj1 already has a property prop created on it. The chained assignment starts from the rightmost =, but the left-hand obj1.prop is already evaluated before that assignment happens.

For an in-depth explanation of chained assignments in JavaScript, refer to this article: https://www.cnblogs.com/xxcanghai/p/4998076.html


Variable Scope and Function Execution Context

var num = 10
function showNum() {
  console.log(num)
}
function execute(fn) {
  var num = 20
  fn()
}
execute(showNum) // 10

When execute(showNum) is called, the execute function receives showNum and invokes it, leading to logging num. The key question is: which num is logged — the global one or the one inside execute?

Although showNum is called inside execute, JavaScript functions always execute in the scope where they were defined, not where they are called. Passing a function as an argument passes its reference. If arguments were used to recreate the function body, the behavior would differ.


Function and Method Context

var logFn = function () {
  console.log(logFn)
}
logFn() // logs the function itself

var container = {
  method: function () {
    console.log(this.method)
  }
}
container.method()

logFn() simply prints its own function body.
container.method() invokes a method on container. Inside a method defined within a plain object, this refers to the object itself. Therefore, it prints the method’s function body.


Variable Hoisting and Function Declarations

var val = 1;
function val(val) {
  console.log(val);
  var val = 3;
}
console.log(val); // 1
val(2); // val is not a function

This tests hoisting: function declarations are hoisted above var declarations. The var val declaration on the first line overrides the function, so val becomes 1.


Immediately Invoked Function Expression (IIFE) and Hoisting

var title = 'World!'
;(function () {
  if (typeof title === 'undefined') {
    var title = 'Jack'
    console.log(title)
  } else {
    console.log(title)
  }
})() // Jack

This examines IIFE and hoitsing. During the pre‑execution parsing stage, any var declaration inside a function is hoisted to the top of that scope. The condition inside the if statement does not prevent hoisting, so title is undefined within the function before assignment.


Event Loop: Synchronous vs Asynchronous Code

var x = 6
setTimeout(function () {
  console.log(0)
  alert(x)
  x = 666
}, 0)
console.log(1)
x = 66

This highlights the execution order of main thread and asynchronous callbacks. Even with a delay of 0, the setTimeout callback is placed in the task queue and runs only after the main thread finishes. Therefore, the alert shows 66.


Prototype Mutation After Instantiation

function Constructor () {
}
Constructor.prototype.val = 1
var instance1 = new Constructor()

Constructor.prototype = {
  val: 2,
  extra: 3
}
var instance2 = new Constructor()

console.log(instance1.val, instance1.extra, instance2.val, instance2.extra) // 1 undefined 2 3

This tests whether previousyl created instances are affected by changing the constructor's prototype afterward. The original prototype object remains referenced by existing instances.


Prototype Chain Lookup for Functions and Objects

var Func = function(){}
Object.prototype.a = function(){
  console.log('a()')
}
Function.prototype.b = function(){
  console.log('b()')
}
var obj = new Func()

Func.a() // a()
Func.b() // b()
obj.a() // a()
obj.b() // obj.b is not a function

This demonstrates that functions are also objects within the prototype chain. Func is an instance of Function, so Func.__proto__ equals Function.prototype, and Function.prototype.__proto__ equals Object.prototype.

Func.a() finds the method on Object.prototype. Func.b() finds the method on Function.prototype. obj.a() reaches Object.prototype. However, obj.b() is not found because obj's prototype is Func.prototype, not Function.prototype, and Object.prototype has no b method, resulting in an error.

Tags: javascript Interview Questions Objects Prototypes scoping

Posted on Thu, 22 Oct 2026 16:08:08 +0000 by roldahayes