JavaScript unshift() Method

 

Definition and Usage

The unshift() method adds new elements to the beginning of an array.

The unshift() method overwrites the original array.


Return Value

TypeDescription
A numberThe new length of the array.


syntex

array.unshift(item1, item2, ..., itemX)



<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <h1>JavaScript Array unshift() Method</h1>
  <script>
    const fruits = [1, 4, 8, 4];
    console.log(fruits.unshift(2));
    console.log(fruits);
  </script>

</body>

</html>

Comments

Popular posts from this blog

Javascript Array Concat and Join Method

JavaScript While Loop

JavaScript pop() Method