Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> JavaScript

JavaScript Array.of() function

Phương thức Array.of() của JavaScript dùng để tạo instance mảng mới với các biến làm giá trị tham số.

Cú pháp:

Array.of(elements....)

Trong đó, elements là các giá trị làm tham số.

Triển khai Array.of() trong JavaScript:

Ví dụ 1

<!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>

Kết quả

JavaScript Array.of() function

Click "Result" và xem Console –

JavaScript Array.of() function

Ví dụ 2

<!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>

Kết quả

JavaScript Array.of() function

Click "Result" và xem Console –

JavaScript Array.of() function