JavaScript Array slice()

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

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

Syntax:

array.slice(start,end)

Parameters: start: It is an optional parameter and represents the index from where the function starts to fetch the elements. end: It is an 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>
Please follow and like us:
Content Protection by DMCA.com