call() method in JavaScript ( #shorts )

1 year ago
1

Hi Friends,

call() method in JavaScript

In JavaScript, `call()` method is used to call a function with a specific `this` value and argument
eg: There is a object `Human` with `name` property & function `welcome()` that will show a welcome message with name of Human. `call()` is use to set the `this` value of `welcome()` to the `Human` object & pass the `name` property as an argument.

const human = {
name: "David",
welcome: function(message) {
console.log(`${message}, ${this.name}!`);
}
};

human.welcome("Hello"); // Output: Hello, David!

// Using call() to call wekcome() with a different message and this value
human.welcome.call({ name: "Sarah" }, "Hi"); // Output: Hi, Sarah!

Using `call()` allows us to reuse the `welcome()` function for different objects and control the `this` value and arguments passed to it.

#javascript #call #callmethod #string #javascriptinterview #method #js #jquery #function #shorts #viral #popular #latest #new #famous #coders #development #code #dev #interview #php #trend #trending #answer #youtube #instagram #socialmedia #education #edit #gaming #how #india #knowledge #love #like #motivational #quiz #reels #short #subscribe #share #update #video #web #webdeveloper #website #webdesign #ytshorts #youtuber #princecheema_official @princecheema_official

Loading comments...