JavaScript Interview Questions and Answers (2025)

author image Hirely
at 05 Jan, 2025

Question: What is the this keyword in JavaScript?

Answer:

The this keyword in JavaScript refers to the context in which a function is called. Its value depends on how a function is invoked and can change dynamically. It is commonly used to access the properties and methods of the current object, but its behavior varies across different execution contexts.Here’s a breakdown of how this works in different situations:

  1. Global Context (Outside of any function) :
  • In the global execution context, this refers to the global object.

  • In browsers, the global object is the window object.

  • Example:

console.log(this);  // In a browser, this refers to the `window` object
  1. Inside a Regular Function (Non-strict mode) :
  • When a function is called in the global context or as a regular function, this refers to the global object (window in browsers).

  • Example:

function greet() {
  console.log(this);  // Refers to the global object (window in browsers)
}
greet();  // `this` will refer to the global object
  1. Inside a Method (Object Context) :
  • When this is used inside an object method, it refers to the object that the method is a part of.

  • Example:

const person = {
  name: 'Alice',
  greet: function() {
    console.log(this.name);  // `this` refers to the `person` object
  }
};
person.greet();  // Output: Alice
  1. Inside a Constructor Function :
  • When a function is used as a constructor (invoked using new), this refers to the newly created object.

  • Example:

function Person(name) {
  this.name = name;
}
const person = new Person('Bob');
console.log(person.name);  // Output: Bob
  1. Arrow Functions :
  • In arrow functions , this does not refer to the function itself. Instead, it inherits the value of this from its surrounding lexical context (i.e., the context in which the arrow function was defined).

  • Example:

const person = {
  name: 'Charlie',
  greet: () => {
    console.log(this.name);  // `this` does NOT refer to `person`
  }
};
person.greet();  // Output: undefined (because `this` is not bound to `person`)
  1. Explicit Binding with .call(), .apply(), and .bind()** :
  • You can explicitly set the value of this using methods like .call(), .apply(), and .bind().
    • .call() and .call() and .apply() immediately invoke the function, passing this and additional arguments.

    • .bind() returns a new function where this is permanently set to a specific value.

  • Example:
function greet() {
  console.log(this.name);
}

const person = { name: 'David' };

greet.call(person);  // Output: David (this is explicitly set to `person`)
  1. In Classes (ES6 and Beyond) :
  • Inside a class method, this refers to the instance of the class .

  • Example:

class Person {
  constructor(name) {
    this.name = name;
  }
  greet() {
    console.log(this.name);  // `this` refers to the instance of the class
  }
}

const person = new Person('Eve');
person.greet();  // Output: Eve

Summary:

  • The value of this in JavaScript is determined by the context in which a function is called.

  • In global context: this refers to the global object (window in browsers).

  • Inside a method: this refers to the object the method is part of.

  • In a constructor: this refers to the new object being created.

  • In an arrow function: this inherits from the surrounding context.

  • Explicit binding: Use .call(), .apply(), or .bind() to set this explicitly. Understanding the behavior of this is essential for mastering JavaScript and for avoiding common pitfalls.

Question: What is the difference between == and === in JavaScript?

Answer:

In JavaScript, == and === are both comparison operators, but they behave differently when comparing values.

  1. == (Loose Equality / Abstract Equality):

    • The == operator performs type coercion. It compares two values for equality after converting them to a common type.
    • This means that if the values are of different types, JavaScript will attempt to convert them to a similar type before making the comparison.

    Example:

    5 == '5' // true (because '5' is coerced to number 5)
    true == 1 // true (because true is coerced to 1)
    null == undefined // true (special case)
  2. === (Strict Equality):

    • The === operator, also known as strict equality, does not perform type coercion. It compares both the value and the type of the operands.
    • If the types are not the same, the comparison immediately returns false.

    Example:

    5 === '5' // false (because one is a number and the other is a string)
    true === 1 // false (because true is a boolean and 1 is a number)
    null === undefined // false (null and undefined are not the same type)

Key Difference:

  • == allows type coercion and can result in true when comparing values of different types.
  • === requires that both the value and type be exactly the same.

In general, it’s recommended to use === to avoid unexpected results due to type coercion.

Read More

If you can’t get enough from this article, Aihirely has plenty more related information, such as JavaScript interview questions, JavaScript interview experiences, and details about various JavaScript job positions. Click here to check it out.

Related Posts

Trace Job opportunities

Hirely, your exclusive interview companion, empowers your competence and facilitates your interviews.

Get Started Now