This in JavaScript

function’s this keyword behaves a little differently in JavaScript compared to other languages. It also has some differences between strict mode and non-strict mode.

In most cases, the value of this is determined by how a function is called (runtime binding). It can’t be set by assignment during execution, and it may be different each time the function is called.

Simply means that we have an object and this object has some functions and inside of this function when we do something we have access to this keyword and this refers to the object that the function is a property of


const tim = {
    name: "Tim",
    country: "USA",
    language: "english",
    speak() {
        return this.name + " can speak " + this.language;
    }
}

console.log(tim.speak())
Tim can speak english
  • speak function can access name and language variable in obj

More Information

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this

Leave a Reply

Your email address will not be published.

ANOTE.DEV