Slicing JavaScript Arrays

To slice an array in JavaScript, you can use the array.slice(start, end) method, which returns a shallow copy of the original array containing the selected elements from the original array. The array.slice() method does not change the original array. The slice() method accepts two optional parameters. The first parameter specifies the starting index from which the elements will be retrieved. If the first parameter is not specified, the method assumes the default value for "start" is "0". The second parameter is the index up to which the elements should be retrieved. If the second parameter is not specified, then the array will be retrieved up to the end of the array. If the end value is greater than the length of the array, then the end value will be equal to the array's length. If the parameters "start" and "end" are negative numbers, then the elements are selected from the end of the array. In this JavaScript Slice Array Example, we use the slice() method. Click Execute to run the JavaScript Array Slice Example online and see the result.
Slicing JavaScript Arrays Execute
let array = ['JavaScript', 'Split', 'Array', 'Example'];

console.log(array.slice(1, 3));
Updated: Viewed: 6784 times

What is JavaScript?

A JavaScript scripting language is used to create powerful web applications in browsers and build high-performance web servers. A primary programming language for the web is JavaScript. Currently, 98% of websites use JavaScript to program the behavior of their website pages, and all major web browsers support it. Also, JavaScript is widely used on the server side (Node.js), allowing developers to write command-line tools and run server-side scripts.

What is a string in JavaScript?

In JavaScript, a string is a sequence of characters, including letters, numbers, and symbols. Strings are primitive and immutable data types in JavaScript, which means they can't be changed after they've been created. Unicode characters can be encoded in JavaScript strings using the "/uXXXX" syntax. JavaScript provides built-in functions for trimming, splitting, containing, concatenating, and reversing strings. You can also check if it starts or ends with another string, convert array to string, convert string to array, create a multiline string, and get the length of a string.

How to slice a JavaScript array using the slice() method?

The array.slice(start, end) method returns a shallow copy of the array containing a portion of the original array. A shallow copy is an exact copy of the original object's values one level deep. This means that if any fields of an object are referred to other objects, only the addresses will be copied, but not the objects themselves. The array.slice() method does not change the original array. The slice() method copies object references to a new array, with the original and the new array referring to the same object. If the object by reference is changed, then the changes will be visible both in the original and in its copy. Changing a string or number in one array does not affect the other in any way. If a new element is added to the original array or copy, it will not affect the different array.

JavaScript Array slice() Syntax
array.slice(start, end)

Where:
  • start (optional): an integer indicates which index to start selecting elements (the first element has index 0). If the parameter is not specified, then the default value will be zero;
  • end (optional): an integer specifying where the selection of array elements ends. If this parameter is not specified, then all elements from the initial position (start) to the end of the array will be selected.

JavaScript Array Slice Examples

The following are some examples of slicing an array in JavaScript:

Copying the entire array

In this example, the array.slice() method copies the entire array since no arguments are passed:

JavaScript Slice Whole Array Example
let array = ['JavaScript', 'Split', 'Array', 'Example'];

console.log(array.slice());

// output: ['JavaScript', 'Split', 'Array', 'Example']

Copying from a specific index up to the end of an array

An example of using the array.slice() method to copy an array from an index to the end of the array:

JavaScript Array Slice from an Index Example
let array = ['JavaScript', 'Split', 'Array', 'Example'];

console.log(array.slice(1));

// output: ['Split', 'Array', 'Example']

Slicing elements from the middle of an array

In this example, the array.slice() method copies the elements from the middle of the given array:

JavaScript Slice from the Middle of Array Example
let array  = ['JavaScript', 'Split', 'Array', 'Example'];

console.log(array.slice(1, 3));

// output: ['Split', 'Array']

Slicing elements from the end of an array

In this example, the array.slice() method copies the elements from the end of the given array:

JavaScript Slice from the End of Array Example
let array  = ['JavaScript', 'Split', 'Array', 'Example'];

console.log(array.slice(-2));

// output: ['Array', 'Example']

Slicing a specific part of an array elements

In this example, the array.slice() method copies certain elements from a given array:

JavaScript Array Slice Specific Elements Example
let array = ['a', 'b', 'c', 1, 2, 3]

console.log(array.slice(0, 3));

// output: ['a', 'b', 'c']

How to slice an array with negative start and end parameters?

An example of using the array.slice() method to slice an array with negative parameters:

JavaScript Array Slice with Negative Parameters Example
let array = ['a', 'b', 'c', 1, 2, 3]

console.log(array.slice(-3, -1));

// output: [1, 2]

See also