JavaScript Array slice() method

The JavaScript array slice() method is used to return a new array containing the copy of the part of the given array.

Note: Original array will not be modified. It will return a new array.

Syntax:

array.slice(start,end)

Parameters:
start: It is optional parameter and represents the index from where the function starts to fetch the elements.
end: It is optional parameter and represents the index at where the function ends fetching elements.

Returns:
A new array with specified elements.

Example:

<!DOCTYPE html>
<html>
<body>
<script>  
var a = ["GOLD","SILVER","DIAMOND","RUBY","PLATINUM"]
var result=a.slice(2,4);  
document.writeln(result);  
</script>  
</body>
</html>

Output:

DIAMOND,RUBY
Please Share