Home >>JavaScript Array Reference >JavaScript Array slice() Method

JavaScript Array slice() Method

JavaScript Array slice() Method

JavaScript slice() method is used to return the selected elements in of the given input array as a new array object. It does not change the original given array.

Syntax:
array.slice(start, end)

Parameter Values

Parameter Description
start This is an optional parameter. It defines an integer that specifies where to start the selection.
end This is an optional parameter. It defines an integer that specifies where to end the selection.

Browser Support

Method Chrome Edge Firefox Safari Opera
slice() Yes Yes Yes Yes Yes
Here is an example of JavaScript slice() method:
<html>
<body>
<p>Click the button to see the Output.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() 
{
  var x = ["0", "1", "2", "3", "4","5"];
  var citrus = x.slice(1, 4);
  document.getElementById("demo").innerHTML = citrus;
}
</script>
</body>
</html>
Output:

Click the button to see the Output.

Example 2:
<html>
<body>
<p>Click the button to see the Output.</p>
<button onclick="myFunction1()">Try it</button>
<p id="demo1"></p>
<script>
function myFunction1() 
{
  var x = ["A","B","H","I","M","A","N"];
  var citrus = x.slice(0, 4);
  document.getElementById("demo1").innerHTML = citrus;
}
</script>
</body>
</html>
Output:

Click the button to see the Output.