JavaScript Array of() Method
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:
- Parameters present are element0, element1, .... which are basically an element for which the array creation is done.
Return Value:
- It simply returns a new Array instance.
Example 1: Creating an Array with Numbers.
In this example, Creates an array with the numbers 1 to 5 using Array.of()
.
JavaScript
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);
Output[ 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);
Output[ undefined, null, NaN, Infinity ]
Supported Browsers:
- Chrome 45
- Edge 12
- Firefox 25
- Opera 26
- Safari 9
Explore
JavaScript Basics
Array & String
Function & Object
OOP
Asynchronous JavaScript
Exception Handling
DOM
Advanced Topics