JavaScript Array.of() function

JavascriptWeb DevelopmentFront End TechnologyObject Oriented Programming

The Array.of() method of JavaScript is used to create a new array instance with variables as parameter values.

The syntax is as follows −

Array.of(elements....)

Above, elements are the values as parameter values.

Let us now implement the Array.of() method in JavaScript −

Example

 Live Demo

<!DOCTYPE html>
<html>
<body>
   <h2>Demo Heading</h2>
   <p>Click the button to display the values in Console</p>
   <button onclick="display()">Result</button>
   <p id="test"></p>
   <script>
      function display() {
         console.log(Array.of(10, 20, 30, 40 ,50));
      }
   </script>
</body>
</html>

Output

Click the “Result” and check the Console for the output −

Example

 Live Demo

<!DOCTYPE html>
<html>
<body>
   <h2>Demo Heading</h2>
   <p>Get the student names with top 5 ranks...</p>
   <button onclick="display()">Result</button>
   <p id="test"></p>
   <script>
      function display() {
         console.log(Array.of("Kevin", "Tom", "Ryan", "Jacob", "Jack"));
      }
   </script>
</body>
</html>

Output

Click the “Result” button and check the result in the Console −

raja
Published on 17-Dec-2019 14:02:53
Advertisements