Javascript Array - slice method
The slice method copies a continuous block of array and retuns it to new array. It can have either no argument, 1 argment or 2 arguments/
- If you specify one no argument, it will return the complete array.
- If you specify one integer argument, it specifies the start of the index from which we need to copy.
- If you specify two arguments, then it tells the begining and end of the index to copy. Take a look at the following example
<html> <body> <script type="text/javascript"> var a = [1,2,3,4,5,6,7,8,9,10]; var x = a.slice(); document.writeln(x+'<BR>'); // outputs: 1,2,3,4,5,6,7,8,9,10 var y = a.slice(4); document.writeln(y+'<BR>'); // outputs: 5,6,7,8,9,10 var z = a.slice(4,7); document.writeln(z+'<BR>'); // outputs: 5,6,7 </script> </body> </html> |
Try this example online here .