javascript get sub array 1

javascript get sub array

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

Here is what the above code is Doing:
1. We create an array called fruits.
2. We create a new array called citrus, which contains elements from the fruits array.
3. The citrus array is created by calling the slice method on the fruits array.
4. The slice method takes two arguments: the index to start at, and the index to end at (but not include).
5. The citrus array is [“Orange”, “Lemon”] because it starts at index 1 (“Orange”) and ends at index 3 (“Lemon”) but does not include it.

Similar Posts