Javascript Slice() Function

The slice() function of array object extracts a section of an array and returns a new array. Use it for example to filter or get section of array.

Syntax

arr.slice(start, [end])

Javascript Slice() Function Example1

<html>
<head>
<script type="text/javascript">
// Create an array.
var arr = [0, 1, 2, 3, 4, 5];
var newArray = arr.slice(2);
document.write(newArray);
</script>
</head>
<body>
</body>
</html>

This will produce following result

2,3,4,5

Javascript Slice() Function Example2

<html>
<head>
<script type="text/javascript">
// Create an array.
var arr = [0, 1, 2, 3, 4, 5, 6, 7];
var newArray = arr.slice(1,6);
document.write(newArray);
</script>
</head>
<body>
</body>
</html>

This will produce following result

1,2,3,4,5