> 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/functions.md).

# Functions

Can have default parameters, no named parameters when calling though

```javascript
function scrape(company, secrets, headless=false, toScrape=["All"]) {
    //...
}
```

Different ways to call ft change how it interacts with the local scope

## Using this

Use the this of the scope that defined it: `.bind(this)` or store the context `var self = this` or use arrow its

## 7 Ways to Define Ft

### Ft Declaration

```javascript
function isEven(num) {
  return num % 2 === 0;
}
```

**Creates** isEven variable in current scope that hold ft obj, **hoists** ft up, and **names** it useful for debugging

Need a name for recursion

### Arrow Ft

Takes

```javascript
const absValue = (number) => {
  if (number < 0) {
    return -number;
  }
  return number;
}
```

### Ft Expression

```javascript
const isTruthy = function(value) {
  return !!value;
};
```

allows conditional setting of variable

Name informed for debugging using variable that holds it

### Named Ft Expression

```javascript
const getType = function funName(variable) {
  console.log(typeof funName === 'function'); // => true
  return typeof variable;
}
```

Name only available in body, and name set as such

### Shorthand method definition

Possible on object literals and classes, required in classes

```javascript
const collection = {
  items: [],
  add(...items) {
    this.items.push(...items);
  },
  get(index) {
    return this.items[index];
  }
};
```

### Generator Ft

```javascript
function* indexGenerator(){
  var index = 0;
  while(true) {
    yield index++;
  }
}
const g = indexGenerator();
console.log(g.next().value); // => 0
console.log(g.next().value); // => 1
```

Can also be used with ft expression, and method

```javascript
const indexGenerator = function* () {
//...
const obj = {
  *indexGenerator() {
//...
```

#### Ft New

This is disgusting don't use, always declared in global scope

```javascript
const global = new Function('return this')();
```


---

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