JavaScript Array.of() Method

Beginner
⏱️ 7 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Static

What You’ll Learn

Array.of() is a static method that builds a new array from the values you pass as arguments. It avoids the confusing behavior of Array(7) (length-only array) versus Array.of(7) ([7]). This tutorial covers syntax, five examples, comparisons, and when to prefer literals or Array.from().

01

Static

Array.of()

02

Arguments

Become elements

03

No quirks

Single number OK

04

Empty call

[]

05

Spread

...values

06

ES2015

Modern browsers

Introduction

Most of the time you create arrays with literals: [1, 2, 3]. But the legacy Array() constructor has a trap: a single number argument sets length, not the element value. Array.of() was added in ES2015 to create arrays predictably from any list of arguments.

Think of Array.of as “make an array containing exactly these values”—whether you pass zero, one, or many arguments.

Understanding the Array.of() Method

Array.of(element0, element1, ...) returns a new dense array. Each argument becomes an element at the next index. There is no special case for a lone number: Array.of(5) is [5], not an array of length 5.

This is the counterpart to Array.from(), which builds arrays from iterables or array-like objects. Use of when you already have individual values; use from when converting another collection.

💡
Beginner Tip

For fixed lists in source code, [1, 2, 3] is still the most readable choice. Reach for Array.of when the constructor quirk matters or when spreading dynamic arguments into a new array.

📝 Syntax

General form of the static Array.of method:

JavaScript
Array.of(element0, element1, /* ... , */ elementN)

Parameters

  • element0, element1, ... — zero or more values; each becomes one array element.

Return value

  • A new Array instance containing the arguments.
  • Array.of() with no arguments returns [].

Common patterns

  • Array.of(1, 2, 3) — list of values.
  • Array.of(42) — single-element array [42].
  • Array.of(...items) — copy spread values into a new array.
  • Array.of(x, y, z) instead of new Array(x, y, z) for clarity.

⚡ Quick Reference

ExpressionResult
Array.of(1, 2, 3)[1, 2, 3]
Array.of(7)[7]
Array(7)Length 7, empty slots
Array.of()[]
Array.from("hi")["h", "i"] (iterable)

📋 Array.of() vs Array() vs [] vs Array.from()

Pick based on whether you have raw arguments, need length-only arrays, or are converting iterables.

Array.of
arguments → array

No length trap

[] literal
[1, 2, 3]

Most readable

Array()
length quirk

Single number

Array.from
iterable

Convert sources

Examples Gallery

Open DevTools Console (F12) or use Try-it labs. Example N maps to ?tryit=N.

📚 Getting Started

Create arrays from explicit values.

Example 1 — Create an Array from Values

Pass multiple arguments; each becomes an element.

JavaScript
const numbers = Array.of(1, 2, 3, 4, 5);

console.log(numbers);
// [1, 2, 3, 4, 5]

console.log(Array.isArray(numbers));
// true
Try It Yourself

How It Works

Same result as [1, 2, 3, 4, 5], but useful when values come from variables or function parameters.

Example 2 — Single Number: Array.of(7) vs Array(7)

The classic reason Array.of exists—avoid the constructor length trap.

JavaScript
console.log(Array.of(7));
// [7] — one element

console.log(Array(7));
// [empty × 7] — length 7, no elements

console.log(Array(7).length);
// 7

console.log(Array.of(7).length);
// 1
Try It Yourself

How It Works

With multiple arguments, Array(3, 4) behaves like Array.of(3, 4). The quirk applies only when there is one numeric argument.

📈 Practical Patterns

Empty arrays, mixed types, and dynamic values.

Example 3 — No Arguments Returns an Empty Array

Safe default when building arrays conditionally.

JavaScript
const empty = Array.of();

console.log(empty);
// []

console.log(empty.length);
// 0
Try It Yourself

How It Works

Unlike Array() or Array(0), you always get a normal empty array with no ambiguous length-only behavior.

Example 4 — Mixed Types in One Array

Any value can be an element—strings, numbers, booleans, even undefined.

JavaScript
const mixed = Array.of("hello", 42, true, null, undefined);

console.log(mixed);
// ["hello", 42, true, null, undefined]

console.log(mixed.length);
// 5
Try It Yourself

How It Works

JavaScript arrays are heterogeneous. Array.of stores each argument as-is without coercion.

Example 5 — Spread Dynamic Values with Array.of(...items)

Collect an unknown number of values into a fresh array.

JavaScript
function toArray(...items) {
  return Array.of(...items);
}

const scores = toArray(88, 92, 76);

console.log(scores);
// [88, 92, 76]

console.log(scores.length);
// 3
Try It Yourself

How It Works

Rest parameters already form an array, but Array.of(...items) is handy when normalizing unknown argument lists into a new copy.

🚀 Common Use Cases

  • Single numeric elementArray.of(n) instead of Array(n).
  • Factory helpers — wrap rest args into a new array copy.
  • Consistent APIs — always return real arrays from functions.
  • Testing — build fixture arrays without constructor surprises.
  • Functional style — pair with spread when composing data pipelines.
  • Teaching — demonstrate static vs instance Array methods.

🧠 How Array.of() Runs

1

Count arguments

Engine reads how many values were passed.

Input
2

Allocate array

Creates a new dense array with that length.

Create
3

Copy each argument

Assigns argument 0 to index 0, and so on.

Fill
4

Return new array

No length-only sparse trap from a single number.

📝 Notes

  • Static method—call Array.of(), not on an instance.
  • Fixes the Array(n) single-number length quirk.
  • Does not convert iterables—use Array.from() for strings, Sets, etc.
  • Literals [a, b, c] remain preferred for fixed lists in source code.
  • Array.of(1, 2) and new Array(1, 2) both yield [1, 2].
  • ES2015—not available in IE without a polyfill.

Browser & Runtime Support

Array.of() was introduced in ES2015 (ES6) alongside other static Array helpers like Array.from() and Array.isArray().

Baseline · ES2015

Array.of()

Supported in Chrome 45+, Firefox 25+, Safari 9+, Edge (all versions), and all modern Node.js versions. Not supported in Internet Explorer.

97% Modern browsers
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
Array.of() Excellent

Bottom line: Safe in all modern projects. Use a polyfill or literals if you must support Internet Explorer.

Conclusion

Array.of() creates predictable arrays from arguments, especially when a single number should be an element, not a length. For converting strings or Sets, use Array.from(); for everyday lists, literals still win on readability.

You have now covered all three core static Array helpers. Return to the array methods hub to explore instance methods like at() and map().

💡 Best Practices

✅ Do

  • Use Array.of(n) when n must be the element value
  • Prefer [] literals for fixed lists in source code
  • Use Array.from for iterables and array-like objects
  • Spread rest parameters when building copies: Array.of(...args)
  • Document why you chose of over a literal in edge cases

❌ Don’t

  • Use Array(n) when you mean [n]
  • Replace every literal with Array.of for no reason
  • Expect Array.of to parse strings into characters
  • Call it on an instance: [1].of(2) does not exist
  • Confuse Array.of with Array.from

Key Takeaways

Knowledge Unlocked

Five things to remember about Array.of()

Create arrays from arguments without constructor surprises.

5
Core concepts
7 02

Single number

[7] not length.

Quirk fix
📦 03

Empty

[]

Default
🔄 04

from

Iterables.

Pair
[] 05

Literals

Still common.

Style

❓ Frequently Asked Questions

Array.of() creates a new array whose elements are the arguments you pass in. Each argument becomes one array element, with no special constructor rules.
It is a static method on the Array constructor. Call Array.of(1, 2, 3), not someArray.of(1, 2, 3).
Array(7) creates an array with seven empty slots (length 7). Array.of(7) creates [7] — one element with value 7. The constructor treats a single number as a length; Array.of always treats arguments as elements.
For everyday code, literals like [1, 2, 3] are fine. Use Array.of when you need predictable behavior with a single numeric value, or when wrapping spread arguments: Array.of(...values).
An empty array: []. It never creates a length-only sparse array.
Array.of() is ES2015 (ES6). It works in all modern browsers and Node.js. Internet Explorer does not support it without a polyfill.
Did you know?

Typed arrays have their own of methods too: Int8Array.of(1, 2, 3) creates a typed array with those values. The same “arguments become elements” rule applies.

Back to Array methods hub

Browse every instance and static Array method in one place.

Array methods overview →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful