slice the last element of an array code example

Example 1: javascript remove last element from array

array.pop();   //returns popped element
//example
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.pop();  // fruits= ["Banana", "Orange", "Apple"];

Example 2: how to remove last element in js

var array = [1, 2, 3, 4, 5, 6];
array.pop();
console.log(array);
//Output in console section:
//[1, 2, 3, 4, 5]

Example 3: get the last item in an array

let array = [0, 1, 2, 3, 4, 5, 6, 7] 
console.log(array.slice(-1));
>>>[7]
console.log(array.slice(-2));
>>>[6, 7]
console.log(array.slice(-3));
>>>[5, 6, 7]

Example 4: how to get the last element of an array

//Lets we have a array called arr
let arr = ["s","fg","d"]
//Lets print the last element
print(arr[arr.length-1])