This website uses cookies to enhance the user experience

JavaScript Interview Questions

23 Questions
JavaScript

JavaScript

Web DevelopmentFrontendBackend

Question 9

What is the 'this' keyword in JavaScript?

Answer:

The 'this' keyword in JavaScript refers to the object it belongs to. Its value depends on where it is used: in a method, it refers to the owner object; alone, it refers to the global object; in a function, it refers to the global object (in non-strict mode) or undefined (in strict mode); and in an event, it refers to the element that received the event. Understanding the behavior of 'this' is essential for writing object-oriented code and managing the context within functions.

Here is an example demonstrating the use of 'this':

var obj = {
    name: 'John',
    greet: function() {
        console.log('Hello, ' + this.name);
    }
};

obj.greet(); // 'Hello, John'

In this example, this.name refers to the name property of obj when greet is called as a method of obj.

Recent job openings

United Kingdom

Remote

Full-time

Rust

Rust

Haskell

Haskell

posted 20 hours ago

Portugal, Lisbon, Lisbon

Remote

Full-time

JavaScript

JavaScript

TypeScript

TypeScript

posted 21 hours ago

United States

Remote

Full-time

Python

Python

JavaScript

JavaScript

+7

posted 22 hours ago

India, Bengaluru, KA

Remote

Full-time

Python

Python

Java

Java

+4

posted 23 hours ago

United Arab Emirates, Dubai, Dubai

Remote

SQL

SQL

posted 23 hours ago