Skip to main content
added 3 characters in body
Source Link
Ran Turner
  • 18.7k
  • 9
  • 60
  • 60

GettingRetrieving the last item in an array is possible via the length property. Since the array count starts at 0, you can pick the last item by referencing the array.length - 1 item

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

Another option is using the new Array.prototype.at() method which takes an integer value and returns the item at that index. Negative integers count back from the last item in the array so if we want the last item we can just pass in -1

const arr = [1,2,3,4]; const last = arr.at(-1); console.log(last); // 4

Another option is using the new findLast method. You can see the proposal here (currently in stage 4)

const arr = [1,2,3,4]; const last = arr.findLast(x => true); console.log(last); // 4

Another option is using the Array.prototype.slice() method which simply returns a shallow copy of a portion of an array into a new array object.

const arr = [1,2,3,4]; const last = arr.slice(-1)[0]; console.log(last); // 4

Getting the last item in an array is possible via the length property. Since the array count starts at 0, you can pick the last item by referencing the array.length - 1 item

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

Another option is using the new Array.prototype.at() method which takes an integer value and returns the item at that index. Negative integers count back from the last item in the array so if we want the last item we can just pass in -1

const arr = [1,2,3,4]; const last = arr.at(-1); console.log(last); // 4

Another option is using the new findLast method. You can see the proposal here (currently in stage 4)

const arr = [1,2,3,4]; const last = arr.findLast(x => true); console.log(last); // 4

Another option is using the Array.prototype.slice() method which simply returns a shallow copy of a portion of an array into a new array object.

const arr = [1,2,3,4]; const last = arr.slice(-1)[0]; console.log(last); // 4

Retrieving the last item in an array is possible via the length property. Since the array count starts at 0, you can pick the last item by referencing the array.length - 1 item

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

Another option is using the new Array.prototype.at() method which takes an integer value and returns the item at that index. Negative integers count back from the last item in the array so if we want the last item we can just pass in -1

const arr = [1,2,3,4]; const last = arr.at(-1); console.log(last); // 4

Another option is using the new findLast method. You can see the proposal here (currently in stage 4)

const arr = [1,2,3,4]; const last = arr.findLast(x => true); console.log(last); // 4

Another option is using the Array.prototype.slice() method which simply returns a shallow copy of a portion of an array into a new array object.

const arr = [1,2,3,4]; const last = arr.slice(-1)[0]; console.log(last); // 4

added 7 characters in body
Source Link
Ran Turner
  • 18.7k
  • 9
  • 60
  • 60

Getting the last item in an array is possible via the length property. Since the array count starts at 0, you can pick the last item by referencing the array.length - 1 item

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

Another option is using the new Array.prototype.at() method which takes an integer value and returns the item at that index. Negative integers count back from the last item in the array so if we want the last item we can just pass in -1

const arr = [1,2,3,4]; const last = arr.at(-1); console.log(last); // 4

Another option is using the new findLast method. You can see the proposal here (currently in stage 4)

const arr = [1,2,3,4]; const last = arr.findLast(x => true); console.log(last); // 4

Another option is using the Array.prototype.slice() method which simply returns a shallow copy of a portion of an array into a new array object.

const arr = [1,2,3,4]; const last = arr.slice(-1)[0]; console.log(last); // 4

Getting the last item in an array is possible via the length property. Since the array count starts at 0, you can pick the last item by referencing the array.length - 1 item

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

Another option is using the new Array.prototype.at() method which takes an integer value and returns the item at that index. Negative integers count back from the last item in the array so if we want the last item we can just pass in -1

const arr = [1,2,3,4]; const last = arr.at(-1); console.log(last); // 4

Another option is using the new findLast method. You can see the proposal here (currently in stage 4)

const arr = [1,2,3,4]; const last = arr.findLast(x => true); console.log(last); // 4

Another option is using the Array.prototype.slice() method which returns a shallow copy of a portion of an array into a new array object.

const arr = [1,2,3,4]; const last = arr.slice(-1)[0]; console.log(last); // 4

Getting the last item in an array is possible via the length property. Since the array count starts at 0, you can pick the last item by referencing the array.length - 1 item

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

Another option is using the new Array.prototype.at() method which takes an integer value and returns the item at that index. Negative integers count back from the last item in the array so if we want the last item we can just pass in -1

const arr = [1,2,3,4]; const last = arr.at(-1); console.log(last); // 4

Another option is using the new findLast method. You can see the proposal here (currently in stage 4)

const arr = [1,2,3,4]; const last = arr.findLast(x => true); console.log(last); // 4

Another option is using the Array.prototype.slice() method which simply returns a shallow copy of a portion of an array into a new array object.

const arr = [1,2,3,4]; const last = arr.slice(-1)[0]; console.log(last); // 4

deleted 3 characters in body
Source Link
Ran Turner
  • 18.7k
  • 9
  • 60
  • 60

RetrievingGetting the last item in an array is possible via the length property. Since the array count starts at 0, you can pick the last item by referencing the array.length - 1 item

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

Another option is using the new Array.prototype.at() method which takes an integer value and returns the item at that index. Negative integers count back from the last item in the array so if we want the last item we can just pass in -1

const arr = [1,2,3,4]; const last = arr.at(-1); console.log(last); // 4

Another option is using the new findLast method. You can see the proposal here (currently in stage 4)

const arr = [1,2,3,4]; const last = arr.findLast(x => true); console.log(last); // 4

Another option is using the Array.prototype.slice() method which returns a shallow copy of a portion of an array into a new array object.

const arr = [1,2,3,4]; const last = arr.slice(-1)[0]; console.log(last); // 4

Retrieving the last item in an array is possible via the length property. Since the array count starts at 0, you can pick the last item by referencing the array.length - 1 item

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

Another option is using the new Array.prototype.at() method which takes an integer value and returns the item at that index. Negative integers count back from the last item in the array so if we want the last item we can just pass in -1

const arr = [1,2,3,4]; const last = arr.at(-1); console.log(last); // 4

Another option is using the new findLast method. You can see the proposal here (currently in stage 4)

const arr = [1,2,3,4]; const last = arr.findLast(x => true); console.log(last); // 4

Another option is using the Array.prototype.slice() method which returns a shallow copy of a portion of an array into a new array object.

const arr = [1,2,3,4]; const last = arr.slice(-1)[0]; console.log(last); // 4

Getting the last item in an array is possible via the length property. Since the array count starts at 0, you can pick the last item by referencing the array.length - 1 item

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

Another option is using the new Array.prototype.at() method which takes an integer value and returns the item at that index. Negative integers count back from the last item in the array so if we want the last item we can just pass in -1

const arr = [1,2,3,4]; const last = arr.at(-1); console.log(last); // 4

Another option is using the new findLast method. You can see the proposal here (currently in stage 4)

const arr = [1,2,3,4]; const last = arr.findLast(x => true); console.log(last); // 4

Another option is using the Array.prototype.slice() method which returns a shallow copy of a portion of an array into a new array object.

const arr = [1,2,3,4]; const last = arr.slice(-1)[0]; console.log(last); // 4

added 15 characters in body
Source Link
Ran Turner
  • 18.7k
  • 9
  • 60
  • 60
Loading
added 23 characters in body
Source Link
Ran Turner
  • 18.7k
  • 9
  • 60
  • 60
Loading
added 359 characters in body
Source Link
Ran Turner
  • 18.7k
  • 9
  • 60
  • 60
Loading
added 3 characters in body
Source Link
Ran Turner
  • 18.7k
  • 9
  • 60
  • 60
Loading
deleted 1 character in body
Source Link
Ran Turner
  • 18.7k
  • 9
  • 60
  • 60
Loading
added 373 characters in body
Source Link
Ran Turner
  • 18.7k
  • 9
  • 60
  • 60
Loading
added 18 characters in body
Source Link
Ran Turner
  • 18.7k
  • 9
  • 60
  • 60
Loading
added 4 characters in body
Source Link
Ran Turner
  • 18.7k
  • 9
  • 60
  • 60
Loading
deleted 15 characters in body
Source Link
Ran Turner
  • 18.7k
  • 9
  • 60
  • 60
Loading
Added alternative options
Source Link
Ran Turner
  • 18.7k
  • 9
  • 60
  • 60
Loading
deleted 162 characters in body
Source Link
Ran Turner
  • 18.7k
  • 9
  • 60
  • 60
Loading
Source Link
Ran Turner
  • 18.7k
  • 9
  • 60
  • 60
Loading