Jsref Of Array
## JavaScript Array.of() Method
The `Array.of()` method creates a new `Array` instance from a variable number of arguments, regardless of the number or type of the arguments.
---
## Quick Example
Convert a set of values into a new array:
```javascript
const myArr = Array.of("YouTip", "Google", "Taobao");
console.log(myArr);
// Output: ["YouTip", "Google", "Taobao"]
```
---
## Definition and Usage
The `Array.of()` method is static, meaning you call it directly on the `Array` constructor (`Array.of()`) rather than on an instance of an array. It allows you to easily convert a list of arguments into a new array.
### Difference between `Array.of()` and the `Array()` Constructor
The key difference between `Array.of()` and the `Array` constructor lies in how they handle a single argument:
* **`Array.of(7)`** creates a new array with a single element: ``.
* **`Array(7)`** creates an empty array with a `length` property of `7` (an array of 7 empty slots).
```javascript
// Single numeric argument behavior
Array.of(7); //
Array(7); // [empty Γ 7]
// Multiple arguments behavior (they behave identically)
Array.of(1, 2, 3); // [1, 2, 3]
Array(1, 2, 3); // [1, 2, 3]
```
---
## Syntax
```javascript
Array.of(element0)
Array.of(element0, element1)
Array.of(element0, element1, /* ... ,*/ elementN)
```
### Parameters
| Parameter | Description |
| :--- | :--- |
| `element0` | **Required.** The first element to include in the new array. |
| `element1, ..., elementN` | **Optional.** Additional elements to include in the new array. |
### Return Value
A new `Array` instance containing the provided arguments as its elements.
---
## Browser Support
The numbers in the table specify the first browser version that fully supports the `Array.of()` method.
| Method | Chrome | Edge | Firefox | Safari | Opera |
| :--- | :--- | :--- | :--- | :--- | :--- |
| **`Array.of()`** | 45.0 | 12.0 | 32.0 | 9.0 | 25.0 |
---
## Technical Details
| Feature | Specification |
| :--- | :--- |
| **Return Value** | An Array object. |
| **JS Version** | ECMAScript 6 (ES6) |
---
## More Examples
### Basic Usage with Different Data Types
`Array.of()` handles any number of arguments of any type, including `undefined` or objects:
```javascript
Array.of(1); //
Array.of(1, 2, 3); // [1, 2, 3]
Array.of(undefined); //
```
### Calling `of()` on Non-Array Constructors (Advanced)
The `Array.of()` method is a generic factory method. It can be called on any constructor function that accepts a single argument representing the length of the new object.
```javascript
function NotArray(len) {
console.log("NotArray called with length:", len);
}
// Array.of() calls NotArray with the number of arguments (3)
// and then assigns the values to the indices.
const result = Array.of.call(NotArray, 1, 2, 3);
// Output: "NotArray called with length: 3"
console.log(result);
// Output: NotArray { '0': 1, '1': 2, '2': 3, length: 3 }
```
If the `this` value passed to `Array.of()` is not a constructor, a standard `Array` object is returned instead:
```javascript
const plainObj = Array.of.call({}, 1);
console.log(plainObj); //
```
YouTip