> For the complete documentation index, see [llms.txt](https://openai.gitbook.io/code-cheatsheets/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://openai.gitbook.io/code-cheatsheets/js.md).

# Javascript

`"use strict";` at the top of files will prompt new compliers to throw more errors like for using undefined variables

Objects and arrays passed by reference

There are packages that work just in the browser, just in react, just for gatsby, or just for node

## Control

```js
isMember ? "$2.00" : "$10.00"

if (c) {s} else if (c) {s} else {s}
```

```js
for (let i = 0; i < cars.length; i++) { 
    text += cars[i] + "<br>";
}

switch(expression) {
    case x:
        code block
        break;
    case y:
        code block
        break;
    default:
        code block
}
```

## Equality

`===` is strict equality with type and strict

```javascript
77 === '77' // false (Number v. String)
undefined === null //false
```

`==` uses type coercion

```javascript
77 == '77' // true
undefined == null //true
```

## Type Conversion

```js
let x = 132;
x.toString(); //="132"
//Noted 132.toString() doesn't work
//toLocaleString better for data and stuff or something

parseInt('42', 10) //42
parseInt('42px', 10) //42
parseInt('10a,a22') //10
parseInt("a") //NaN - Boolean(NaN) => False
```

Find type with `typeof(var)`

### Random Integer

Math.random gives number between 0(inclusive) and 1(exclusive).

```js
Math.floor(Math.random() * 3) 
```

### DocString

* With this specific commenting above a function, VSCode and other IDEs will show the comment on hover

```ts
/**
 * This script searches all organizations PriorAuths
 */
async function main() {
```

### Why Javascript Sucks

[Can use a variable before it is initalized with no error](https://www.w3schools.com/js/js_hoisting.asp)

```js
Boolean([]) //true
Boolean({}) //true
```

Additionally, some array methods cannot find `NaN`, while others can.

```javascript
let arr = [2, 4, NaN, 12];
arr.indexOf(NaN);                      // -1 (false)
arr.includes(NaN);                     // true
arr.findIndex(n => Number.isNaN(n));   // 2
```

```js
.01 + .01 + .01 //is not .03
```

```js
typeof null; // 'object'
typeof undefined; // 'undefined'
```


---

# 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://openai.gitbook.io/code-cheatsheets/js.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.
