
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
JavaScript Array slice()
The slice() method of JavaScript is used to return the selected elements in an array.
The syntax is as follows −
array.slice(start, end)
Above, start parameter is an integer specifying where to begin the selection, whereas, end is where the selection ends.
Let us now implement the slice() method in JavaScript −
Example
<!DOCTYPE html> <html> <body> <h2>Demo Heading</h2> <p id="test"></p> <script> var arr = ["Accessories", "Electronics", "Books", "Pet Supplies"]; document.getElementById("test").innerHTML = "Initial array = "+arr+", <br>Sliced = "+arr.slice(2, 4); </script> </body></html>
Output
Example
<!DOCTYPE html> <html> <body> <h2>Car Variant</h2> <button onclick="display()">Result</button> <p id="test"></p> <script> var car = ["Hatchback", "Convertible", "SUV", "AUV", "MUV"]; document.getElementById("test").innerHTML = "Initial Array = "+car; function display() { document.getElementById("test").innerHTML = "Array after slicing some elements = "+car.slice(1,4); } </script> </body></html>
Output
Click “Result” button −
- Related Articles
- Array slice() in JavaScript
- Find Max Slice Of Array | JavaScript
- How to slice an array with wrapping in JavaScript
- MongoDB Aggregation to slice array inside array
- Array slice function in Ruby
- MongoDB slice array in populated field?
- Golang program to convert array into slice
- Golang program to convert slice into array
- MongoDB query to slice only one element of array
- what is the use of slice() method in JavaScript?
- MongoDB aggregate $slice to get the length of the array
- Empty Slice vs. Nil Slice in Golang
- Low level difference between Slice and Splice methods in Javascript
- How to Get a slice of a primitive array in Java?
- How to use $slice operator to get last element of array in MongoDB?

Advertisements