Show:
Can You Slice in JavaScript?
An array in JavaScript is an ordered list of elements with a specified index (the key to them). When we want to store a list of certain parts and then access them with a single variable, that’s when we use an array. To work with an array, developers usually apply the built-in functions provided like JavaScript slice.
Unlike other languages, where an array is a reference to multiple variables, in JavaScript it is the only variable that can store multiple pieces:
- It stores values of mixed types, which can contain numbers, strings, and units from other arrays;
- Its length is dynamic. We do not need to specify the size of the array in advance – it changes (increases/decreases) automatically;
- It is used to hold multiple values in one variable.
A pair of square brackets [] denotes an array, the pieces are divided by commas, while the location of an element is indicated by an index. So, the power of JS arrays lies in their methods. They make it possible to manage, sort, transform, and perform the necessary data manipulations when required. Each method has a unique function that makes it possible to modify arrays. Below, we will briefly consider some of the most popular methods, and then learn in detail the peculiarities of the JavaScript slice method.
Array processing tools
There is a variety of built-in tools, which are necessary for working with JavaScript arrays, and which also greatly simplify writing code, making it cleaner. Let’s pay closer attention to the most popular of them:
- Join() – it returns a new string where all the pieces of the array are joined. The main separating sign is a comma. This approach does not change the initial array;
- Reverse() – it reverses the positions of the pieces, the ultimate element becomes the first in a line and vice versa;
- Sort() – this is probably the most frequently-used method as well. It sorts the elements in ascending or descending order. The regular sorting takes place in an alphabetical sequence. The sort() function transforms the initial array;
- Concat() – it concatenates one or several arrays, then returns the concatenated one. The existing arrays remain unchanged;
- Splice() – it helps you add and remove pieces in an array, so, the original one is modified. To add an element using splice(), we need to indicate the position at which we want to add elements;
- Push() and pop() – these are two methods with the opposite functionality. Push() allows inserting an element at the end of an array, so, its length changes. Pop() – removes an element from the end of an array;
- Unshift() and shift() – the first one adds new pieces at the beginning of the array and changes its length. The shift() method removes the first element (the length of the array changes).
However, there is still a question: “What is the Slice method in JavaScript?” Let’s review it in more detail.
Slice Array Method in JavaScript
JavaScript built-in methods really help a lot in the development process, especially if we understand how they work properly. The easiest way to extract a slice of an array, or rather cut it into pieces, is with the slice() method:
- Copies elements from an array;
- Returns them as a new array;
- Does not modify the initial array;
- Starts slicing from … to the given index: array.slice (from, until);
- The last parameter is not sliced by the Slice() method;
- Can be used for both arrays and strings.
So, the slice function slices and returns the specified part of the array. The first parameter specifies the number of the array element from which the cutting starts, and the second parameter is the number of the element where the cutting ends. The second parameter is optional. If it is not specified, the subarray will be taken from the element specified in the first parameter to the end of the array.
The second parameter can also take negative values. In this case, the element at which the cut ends is counted from the end of the array. Moreover, the last element has the number -1, the penultimate -2, and so on. For example, if you write slice(1, -1), then the cut part will be taken from the first element, inclusive, to the last, excluding it. If you need to include the last element as well, you just need to leave out the second parameter for a slice, like this: slice(1). The method does not modify the original array. Let us also remind you that in an array, the numbering of elements starts from zero.
Array in JavaScript: Slice vs. Splice
In JavaScript, using slice instead of the splice and vice versa is a common mistake among beginners and even professionals. Although both methods are very similar in name, they perform two completely different actions. In practice, this error can be avoided by choosing an API that will analyze the correctness of the function entry.
An important feature of the slice method is that it does not change the array it uses, while the splice method works vice versa. When creating your software unit, it’s essential to choose an API that minimizes the confusion with slice and splice. Ideally, the user of your module should be able to decide which of these methods they need without additional reading of the documentation. What symbols should be chosen?
It is quite common to symbolize such methods with a specific form of the verb. For example, a verb in the present simple tense indicates an action that can modify the object of application, and a past participle returns a new version without modifying the object.
Now you know how to use the Slice method and how it differs from the other JS methods. We have also answered the question of if you can use Slice in JS. Of course, there are other built-in JavaScript methods for working with arrays and strings. If you understand how to use them, they will greatly simplify your development process. What JS tools do you usually use when coding? Share with us in the comments.