return last element of an array javascript without removing it code example

Example 1: last element of an array javascript

var myArray = [1, 2, 3, 4];
myArray[myArray.length - 1];

Example 2: 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 3: javascript array remove last

// example (remove the last element in the array)
let yourArray = ["aaa", "bbb", "ccc", "ddd"];
yourArray.pop(); // yourArray = ["aaa", "bbb", "ccc"]

// syntax:
// <array-name>.pop();