# JavaScript Cheatsheet

## Array methods

![Array methods categories](https://1944679227-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MVEiPUp08kYt33g51v7%2F-MXPo2KuiMFTHIJwbu4V%2F-MXPoLn4BiHtgfD-Ym53%2Fimage.png?alt=media\&token=517b59f3-1b89-4745-b190-527876abc882)

## 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>" %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://sandeepamaranath.gitbook.io/notes/quick-access/javascript-cheatsheet.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
