---
title: "thisArg in JavaScript"
description: "Understanding the thisArg parameter in JavaScript array methods. Practical notes and code examples on thisArg in JavaScript."
url: "https://h3manth.com/scribe/thisarg-in-javascript/"
canonical_url: "https://h3manth.com/scribe/thisarg-in-javascript/"
markdown_url: "https://h3manth.com/scribe/blog/thisarg-in-javascript.md"
publishDate: "2014-08-09T00:00:00.000Z"
author: "Hemanth HM"
category: "javascript"
tags: ["javascript"]
word_count: 166
reading_time_minutes: 1
x_markdown_tokens: 425
content_signal: "ai-train=yes, search=yes, ai-input=yes"
---

# thisArg in JavaScript

> Understanding the thisArg parameter in JavaScript array methods. Practical notes and code examples on thisArg in JavaScript.

Not many are aware of `thisArg` that several built-in array methods have in their signature.

If a `thisArg` parameter is provided, it will be used as the `this` value for each invocation of the callback function. If not provided, `undefined` is used instead.

> **Note:** If the callback is an Arrow Function, `this` was lexically bound when the function was created, so `thisArg` will have no effect.

## Methods with thisArg

- `Array.from(arrayLike [, mapfn [, thisArg]])`
- `Array.prototype.every(callbackfn [, thisArg])`
- `Array.prototype.filter(callbackfn [, thisArg])`
- `Array.prototype.find(predicate [, thisArg])`
- `Array.prototype.findIndex(predicate [, thisArg])`
- `Array.prototype.forEach(callbackfn [, thisArg])`
- `Array.prototype.map(callbackfn [, thisArg])`
- `Array.prototype.some(callbackfn [, thisArg])`

## Example: forEach

```javascript
let jedi = {
  name: 'yoda',
  height: '66cms',
  mass: '17 kgs'
};

### Without thisArg
javascript
Object.keys(jedi).forEach(function(key) {
  console.log(jedi[key]);
});

### With thisArg
javascript
Object.keys(jedi).forEach(function(key) {
  // |this| now refers to `jedi`
  console.log(this[key]);
}, jedi); // last arg is `thisArg`
```

The same could be achieved with `bind`, but it would be slower. Check the [performance comparison](http://jsperf.com/foreach-with-thisarg).
