JavaScript Interview Questions and Answers (2025)
Try Aihirely for
Smarter Interview Prep
 Experience real-time AI support tailored to your Resume.
Boost your confidence and ace every question with
AI Mock Interview.
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:
- Global Context (Outside of any function) :
 
- 
In the global execution context,
thisrefers to the global object. - 
In browsers, the global object is the
windowobject. - 
Example:
 
console.log(this);  // In a browser, this refers to the `window` object
- Inside a Regular Function (Non-strict mode) :
 
- 
When a function is called in the global context or as a regular function,
thisrefers to the global object (windowin browsers). - 
Example:
 
function greet() {
  console.log(this);  // Refers to the global object (window in browsers)
}
greet();  // `this` will refer to the global object
- Inside a Method (Object Context) :
 
- 
When
thisis 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
- Inside a Constructor Function :
 
- 
When a function is used as a constructor (invoked using
new),thisrefers to the newly created object. - 
Example:
 
function Person(name) {
  this.name = name;
}
const person = new Person('Bob');
console.log(person.name);  // Output: Bob
- Arrow Functions :
 
- 
In arrow functions ,
thisdoes not refer to the function itself. Instead, it inherits the value ofthisfrom 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`)
- Explicit Binding with 
.call(),.apply(), and.bind()** : 
- 
You can explicitly set the value of
thisusing methods like.call(),.apply(), and.bind().- 
.call()and.call()and.apply()immediately invoke the function, passingthisand additional arguments. - 
.bind()returns a new function wherethisis 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`)
- In Classes (ES6 and Beyond) :
 
- 
Inside a class method,
thisrefers 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
thisin JavaScript is determined by the context in which a function is called. - 
In global context:
thisrefers to the global object (windowin browsers). - 
Inside a method:
thisrefers to the object the method is part of. - 
In a constructor:
thisrefers to the new object being created. - 
In an arrow function:
thisinherits from the surrounding context. - 
Explicit binding: Use
.call(),.apply(), or.bind()to setthisexplicitly. Understanding the behavior ofthisis 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.
- 
==(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) - The 
 - 
===(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) - The 
 
Key Difference:
==allows type coercion and can result intruewhen 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.
