javascript
  1. javascript-of

JavaScript of()

The of() method is a static method of the built-in Array object in JavaScript. It creates a new Array instance with a variable number of arguments. It accepts any data type as its arguments and allows you to create an array from those arguments.

Syntax

The syntax for of() method is:

Array.of(element0[, element1[, ..., elementN]])

Example

const myArray = Array.of(1, 'two', false);
console.log(myArray); // Output: [1, "two", false]

Output

In the above example, the of() method creates a new array instance and initializes it with three elements: the number 1, the string 'two', and the boolean value false. The output will be [1, "two", false].

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Array Example</title>
</head>
<body>

<!-- Display area for the output -->
<div id="output"></div>

<script>
    // Your JavaScript code
    const myArray = Array.of(1, 'two', false);
    console.log(myArray); // Output: [1, "two", false]

    // Display the output in the HTML document
    document.getElementById('output').innerHTML = myArray;
</script>

</body>
</html>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Array Example</title>
</head>
<body>

<!-- Display area for the output -->
<div id="output"></div>

<script>
    // Your JavaScript code
    const myArray = Array.of(1, 'two', false);
    console.log(myArray); // Output: [1, "two", false]

    // Display the output in the HTML document
    document.getElementById('output').innerHTML = myArray;
</script>

</body>
</html>
Try Playground

Explanation

The of() method is used to create an array with a variable number of arguments. It is a new and improved way of creating an array, compared to the traditional method Array(). It is a static method, which means that we can call it directly from the Array object itself, without the need to create a new instance of the Array object.

Use

The of() method can be used whenever you need to create a new array with a variable number of elements. It can be particularly useful when you have a set of arguments that need to be inserted into an array as elements. It's also useful in situations where you need to create an array with only one element.

Important Points

The following are some important points related to the of() method:

  • of() method is a static method of Array object, so it is called directly from Array object.
  • It returns a new Array instance without modifying the original array.
  • It is recommended over other ways of creating arrays since it has better support for creating arrays with a single non-iterable element.

Summary

The of() method is a new and improved way of creating an array with a variable number of arguments. It is a static method of the Array object and can be used whenever you need to create a new array. With its simplicity and flexibility, the of() method is a powerful addition to the array manipulation capabilities of JavaScript.

Published on: