What is the difference between call and apply in JavaScript?
Hello colleagues! I started learning JavaScript and came across the call and apply methods. I read the documentation but still don't fully understand the difference. I have, say, an object:
```javascript
const user = {
name: 'Ivan',
greet: function(greeting, punctuation) {
console.log(`${greeting}, ${this.name}${punctuation}`);
}
};
const admin = {
name: 'Petr'
};
```
I want to call the `greet` method on behalf of the `admin` object. As far as I understand, both methods allow setting `this`. But when I try:
```javascript
user.greet.call(admin, 'Hello', '!'); // works
user.greet.apply(admin, ['Hello', '!']); // also works
```
Is the only difference that `apply` takes an array of arguments? Or are there other nuances? Does this affect performance? Or are there cases where
No answers yet.
Log in to leave an answer
Blogs