Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

slice js

// slice method 

const FRUITS = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
const favFruites=FRUITS.slice(0,1);
const favFruite2=FRUITS.slice(0,3);
const favFruite3=FRUITS.slice(-4);
const favFruite4=FRUITS.slice(-4,-2);


console.log(favFruites);
console.log(favFruite2);
console.log(favFruite3);
console.log(favFruite4);

// output is=

// [ 'Banana' ]
// [ 'Banana', 'Orange', 'Lemon' ]        
// [ 'Orange', 'Lemon', 'Apple', 'Mango' ]
// [ 'Orange', 'Lemon' ]


// note:  its start print value from starting index but not print last indexnumber  


// Returns a copy of a section of an array. For both start and end,
// a negative index can be used to indicate an offset from the end of the array.
//  For example, -2 refers to the second to last element of the array.
Source by education.launchcode.org #
 
PREVIOUS NEXT
Tagged: #slice #js
ADD COMMENT
Topic
Name
9+7 =