> For the complete documentation index, see [llms.txt](https://ploegert.gitbook.io/til/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ploegert.gitbook.io/til/programmy/javascript/create-an-array-containing-1-to-n.md).

# Create An Array Containing 1 To N

Some languages, such as Ruby, have a built in range constraint that makes it easy to construct an array of values from 1 to N. JavaScript is not one of those languages. Nevertheless, if you don't mind the aesthetics, you can get away with something like this:

```javascript
> Array.apply(null, {length: 10}).map(Number.call, Number);
=> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
```

That gives us `0` through `9`. To get `1` through `10`, we can tweak it slightly:

```javascript
> Array.apply(null, {length: 10}).map(Number.call, n => Number(n) + 1);
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
```

To generalize this, we can replace `10` with `N` and then just expect that `N` will be defined somewhere:

```javascript
> var N = 10;
=> undefined
> Array.apply(null, {length: N}).map(Number.call, n => Number(n) + 1);
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
```

[Source](http://stackoverflow.com/a/20066663/535590)


---

# 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://ploegert.gitbook.io/til/programmy/javascript/create-an-array-containing-1-to-n.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.
