JavaScript slice: Main Tips
- This JavaScript slice method is used to return selected elements of an array into a new array object.
- The original array is not affected by this method: a new one is created instead.
Method Explained
Before defining the JS slice method, we should briefly remind you that JavaScript arrays contain multiple types of data. If developers wish to perform certain operations with them, they turn to the array slice function. The method returns selected elements of an array into a new array object. However, the original array remains unchanged.
This JS slice function selects elements starting at the one indexed by start argument, and finishes at (but does not include) the element indexed by the end argument:
var carArray =['Mercedes-Benz', 'BMW', 'Audi'];
var modelArray = carArray.slice(0, 2);
- Easy to use with a learn-by-doing approach
- Offers quality content
- Gamified in-browser coding experience
- The price matches the quality
- Suitable for learners ranging from beginner to advanced
- Free certificates of completion
- Focused on data science skills
- Flexible learning timetable
- Simplistic design (no unnecessary information)
- High-quality courses (even the free ones)
- Variety of features
- Nanodegree programs
- Suitable for enterprises
- Paid Certificates of completion
- A wide range of learning programs
- University-level courses
- Easy to navigate
- Verified certificates
- Free learning track available
- University-level courses
- Suitable for enterprises
- Verified certificates of completion
Standard Syntax
Learning the rules of writing JavaScript slice function should not be an issue. Even rookie programmers should be able to quickly memorize the way this method should be used. Remember the correct capitalization and the possible parameters (which are indicated in the parentheses):
array.slice(start,end)
The good news is that the JavaScript slice function does not have any required parameters. Nevertheless, to make the function precise, you need to indicate where the selection should begin and end.
Even though these arguments are optional, we predict that you will be using them to specify the starting index and the end of the collection process.
Parameter | Description |
---|---|
start | Whole number used to specify initial index. Negative values can be used to select from end of an array. 0 by default. |
end | Whole number used to specify last index for extraction (not included). If omitted, all element starting with the element specified by the start parameter are going to be selected. Negative values can be used to select from end of an array. |
Note: keep in mind that the first array element will have the index of 0, not 1.
Return Value
Understanding the return value of this JavaScript slice function does not take a lot of effort. The method will return a new array containing some part of the original array.
When the optional parameters are not included in the parentheses, the JavaScript slice function retrieves the whole array from the specified string. When arguments are specified, the function extracts the array from the indicated index to the end of the array or the specified index.
Return Value: | A new array which contains only the selected elements |
---|---|
JavaScript Version: | ECMAScript 1 |