C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
JavaScript Array slice() methodThe JavaScript array slice() method extracts the part of the given array and returns it. This method doesn't change the original array. SyntaxThe slice() method is represented by the following syntax: array.slice(start,end) Parameterstart - It is optional. It represents the index from where the method starts to extract the elements. end - It is optional. It represents the index at where the method stops extracting elements. ReturnA new array contains the extracted elements. JavaScript Array slice() method exampleHere, we will understand slice() method through various examples. Example 1Let's see a simple example to extract an element from the given array. <script> var arr=["AngularJS","Node.js","JQuery","Bootstrap"] var result=arr.slice(1,2); document.writeln(result); </script> Output: Node.js Example 2Let's see one more example to extract various element from the given array. <script> var arr=["AngularJS","Node.js","JQuery","Bootstrap"] var result=arr.slice(0,3); document.writeln(result); </script> Output: AngularJS,Node.js,JQuery Example 3In this example, we will provide the negative values as index to extract elements. <script> var arr=["AngularJS","Node.js","JQuery","Bootstrap"] var result=arr.slice(-4,-1); document.writeln(result); </script> Output: AngularJS,Node.js,JQuery
Next TopicJavaScript Array
|