js sub array code example

Example 1: js subarray

let subArray = array.slice(startIndex, endIndex)
// ['a', 'b', 'c'].slice(0, 2) === ['a', 'b']

Example 2: javascript slice array

// array.slice(start, end)
const FRUITS = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
var citrus = FRUITS.slice(1, 3);
// citrus => [ 'Orange', 'Lemon' ]

// Negative values slice in the opposite direction
var fromTheEnd = FRUITS.slice(-3, -1);
// fromTheEnd => [ 'Lemon', 'Apple' ]

Example 3: javascript get sub array

var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
var citrus = fruits.slice(1, 3);
// ["Orange", "Lemon"]

Example 4: array index javascript show only first 2 elements

array.slice(0, n);

Tags:

Java Example