> For the complete documentation index, see [llms.txt](https://sandeepamaranath.gitbook.io/notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://sandeepamaranath.gitbook.io/notes/quick-access/javascript-cheatsheet.md).

# JavaScript Cheatsheet

## Array methods

![Array methods categories](/files/-MXPoLn4BiHtgfD-Ym53)

## Test if a variable is an Array

```javascript
const arr = [1,2,3]

// Method 1

Array.isArray(arr) // true

// Method 2

console.log(arr instanceof Array) // true // but also note  - 
// arr instanceof Object  is also true, so you can do this below

if(Array.isArray(arr) && arr instanceof Array) // actually Array.isArray is sufficient


```

## Test if a variable is an Object

```javascript
// This is tricky because array is an object, function is an object so it's difficult
// to find the actual object

// Problem
const arr = []
const fun = function(){}
const actualObj = {}

arr instanceof Object // true
fun instanceof Object // true
actualObj instanceof Object // true


// So the goal is actualObj must return true and everything else false
actualObj instanceof Object && !(actualObj instanceof Array) && !(typeof actualObj === 'function')
```

{% embed url="<https://stackoverflow.com/questions/8511281/check-if-a-value-is-an-object-in-javascript?page=2&tab=oldest#tab-top>" %}
