Code has been added to clipboard!

slice

Reading time 3 min
Published Sep 8, 2017
Updated Oct 10, 2019

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:

Example
var carArray =['Mercedes-Benz', 'BMW', 'Audi'];
var modelArray = carArray.slice(0, 2);

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
What Is JavaScript Used For?
Tutorial
Introduction
Output
Syntax
Comment
Commands
Operators
Comparison and Logical Operators
Data Types
Math.random()
Type Conversion
Function Definitions
Events
Objects
Object Properties
Prototype
Array
Sorting Arrays
Strings
Numbers
Number Format
Math Object
Onclick Event
Date
Date Formats
Scope
Regular Expressions
Reserved Words
Common Mistakes
Performance
Forms
Form Validation
Window: The Browser Object Model
Popup Boxes
Cookies
JSON
AJAX Introduction
AJAX Form
Automatic File Download
Functions
Array Methods
String Methods
Date Methods
Timing Events
Cheat Sheet
JavaScript in HTML
HTML DOM Methods
HTML DOM Changing HTML
HTML DOM Animation
HTML DOM EventListener
HTML DOM Navigation
HTML DOM NodeList
HTML DOM Element Nodes
Array Functions
Boolean
Calling a Function
Date Functions
Global Objects
Input Text
Operator
Statements
String Functions
Math
Math.random
Number
RegEx
alert
array.filter
array.length
array.map
array.reduce
array.push
array.sort
break and continue
className
confirm
decodeURIComponent
for
forEach
if
indexOf
innerHTML
location.reload
number.toString
onclick
onload
parseInt
prompt
replace
setAttribute
setInterval
setTimeout
slice
splice
string.includes
string.indexOf
string.split
style.display
submit
substr
substring
switch
test
throw, try and catch
toLowerCase
toUpperCase
use strict
while
window.history
window.location
window.navigator
window.screen