How to use Array.length In Javascript

The array length property in JavaScript is used to set or return the number of elements in an array. 

Syntax:

array.length

Example: myArray.length returns the current length of the array. By setting myArray.length = myArray.length - 1, you effectively remove the last item from the array.

Javascript
let myArray = [1, 2, 3, 4, 5];

// Use the length property to remove the last item
myArray.length = myArray.length - 1;

console.log(myArray); // Output: [1, 2, 3, 4]

Output
[ 1, 2, 3, 4 ]

Remove the last Item From an Array in JavaScript

Removing the last item from an array in JavaScript is a fundamental operation often encountered in array manipulation tasks. It involves eliminating the last element of an array, which can be useful for dynamically adjusting array contents based on various conditions or requirements within a program.

Methods to Remove the Last Element from an Array:

Table of Content

  • Using Array splice() Method
  • Using Array slice() Method
  • Using Array pop() Method
  • Using array.reduce() Method
  • Using Array.length
  • Using Array.filter() Method

Similar Reads

Method 1: Using Array splice() Method

This method uses Array.splice() method that adds/deletes items to/from the array and returns the deleted item(s)....

Method 2: Using Array slice() Method

In this appraoch we are using Array.slice() method. This method returns a new array containing the selected elements. This method selects the elements that start from the given start argument and end at, but excludes the given end argument....

Method 3: Using Array pop() Method

In this appraoch, we are using Array.pop() method. This method deletes the last element of an array and returns the element....

Method 4: Using array.reduce() Method

The array.reduce() method in JavaScript is used to reduce the array to a single value and executes a provided function for each value of the array and the return value of the function is stored in an accumulator....

Method 5: Using Array.length

The array length property in JavaScript is used to set or return the number of elements in an array....

Method 6: Using Array.filter() Method

The Array.filter() method creates a new array with all elements that pass the test implemented by the provided function. We can use this method to exclude the last element from the array....