Program to use JavaScript Array slice() method
let a = [ 'Cat', 'Dog', 'Tiger', 'Lion', 'Elephant' ];
let b = a.slice(1,4);
console.log('String slice =',b);
Output
String slice = [ 'Dog', 'Tiger', 'Lion' ]
Steps Description
- Declare two variable
a
andb
- Initialize two variable
a
andb
with value of arraylet a = [ 'Cat', 'Dog', 'Tiger', 'Lion', 'Elephant' ]
andb
slice()
method is used to slice the array by giving index number- The value of
b
variable is displayed with the help of console.log inbuilt function
Program to use JavaScript Array slice() method with multiplication table
let c = [ 3,6,9,12,15,18,21,24,27,30 ];
let d = c.slice(5);
console.log('String slice =',d);
Output
String slice = [ 18, 21, 24, 27, 30 ]
Steps Description
- Declare two variable
c
andd
- Initialize two variable
c
andd
with value of arraylet c = [ 3,6,9,12,15,18,21,24,27,30 ]
andd
slice()
method is used to slice the array by giving index number- The value of
d
variable is displayed with the help of console.log inbuilt function
Program to use JavaScript Array slice() method with count
let e = [ 1,2,3,4,5,6,7,8,9,10 ];
let f = e.slice(3,6);
console.log('String slice =',f);
Output
String slice = [ 4, 5, 6 ]
Steps Description
- Declare two variable
e
andf
- Initialize two variable
e
andf
with value of arraylet e = [ 1,2,3,4,5,6,7,8,9,10 ]
andf
slice()
method is used to slice the array by giving index number- The value of
f
variable is displayed with the help of console.log inbuilt function