Js get last element

Code examples

99
0

get last element of array javascript

// Method - 1 ([] operator)
const arr = [5, 3, 2, 7, 8];
const last = arr[arr.length - 1];
console.log(last);
/*
    Output: 8
*/

// Method - 2 (Destructuring Assignment)
const arr = [5, 3, 2, 7, 8];

const [last] = arr.slice(-1);
console.log(last);
/*
    Output: 8
*/

// Method - 3 (Array.prototype.pop())
const arr = [5, 3, 2, 7, 8];

const last = arr.slice(-1).pop();
console.log(last);
/*
    Output: 8
*/

// Method - 4 (Underscore/Lodash Library)
const _ = require("underscore");

const arr = [5, 3, 2, 7, 8];
const last = _.last(arr);
console.log(last);
/*
    Output: 8
*/
90
0

javascript get last element of array

var foods = ["kiwi","apple","banana"];
var banana = foods[foods.length - 1]; // Getting last element
9
0

how to get last item in array javascript

let nums = [1,2,3,4,5];
let lastOne = nums.pop();
// -> lastOne = 5
// -> nums = [1,2,3,4];
9
0

javascript last element of array

let arr = ['a', 'b', 'c']
arr.slice(-1)[0] // returns last element in an array
5
0

javascipt get last element of array

const arr = [1,2,3] ;
console.log(arr[arr.length - 1]);

In other languages

This page is in other languages

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................
Балгарскі
..................................................................................................................
Íslensk
..................................................................................................................