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


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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.
