Last Updated : 15 Jul, 2024
The Javascript array.of() method is an inbuilt method in JavaScript that creates a new array instance with variables present as the argument of the method.
Syntax:Array.of(element0, element1, ....)Parameters:
Example 1: Creating an Array with Numbers.
In this example, Creates an array with the numbers 1 to 5 using Array.of()
.
let numbersArray = Array.of(1, 2, 3, 4, 5);
console.log(numbersArray);
Example 2: Creating an Array with Different Types of Elements
In this example, Creates an array with mixed types: number, string, boolean, object, and array.
JavaScript
let mixedArray = Array.of(10, 'hello', true, { name: 'John' }, [1, 2, 3]);
console.log(mixedArray);
[ 10, 'hello', true, { name: 'John' }, [ 1, 2, 3 ] ]
Example 3: Creating an Array with Undefined and Null Values
In this example, Creates an array containing special values: undefined, null, NaN, and Infinity.
JavaScript
let specialValuesArray = Array.of(undefined, null, NaN, Infinity);
console.log(specialValuesArray);
[ undefined, null, NaN, Infinity ]Supported Browsers:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4