JavaScript Array Last Item

Home /

Table of Contents

Access Array Elements in JavaScript

In JavaScript, you can access individual elements of an array by specifying their index. Array indices start at 0.

Here’s an example code snippet in JavaScript to access array elements:

JavaScript
const myArray = ['apple', 'banana', 'orange'];

// Accessing the first element of the array
const firstElement = myArray[0];

// Accessing the third element of the array
const thirdElement = myArray[2];

console.log(firstElement); // Output: apple
console.log(thirdElement); // Output: orange

In this example, we have an array called myArray with three elements. We access the first element of the array by using the index 0, which gives us the value 'apple'. We access the third element of the array by using the index 2, which gives us the value 'orange'.

You can also use a loop to access all the elements of an array. For example, you can use a for loop to iterate over the array and print each element:

JavaScript
const myArray = ['apple', 'banana', 'orange'];

for (let i = 0; i < myArray.length; i++) {
  console.log(myArray[i]);
}

This code will output all the elements of the myArray array:

apple
banana
orange

Insert Item into an Array at a Specific Index

In JavaScript, you can insert an item into an array at a specific index using the ‘splice()‘ method. Here’s an example code snippet:

JavaScript
const originalArray = [1, 2, 3, 4, 5];
const indexToInsert = 2;
const elementToInsert = 99;

originalArray.splice(indexToInsert, 0, elementToInsert);

console.log(originalArray); 

Output:

1, 2, 99, 3, 4, 5

Delete Item from Array

In JavaScript, you can delete an item from an array using the splice() method. Here’s an example code snippet:

JavaScript
const originalArray = [1, 2, 3, 4, 5];
const indexToDelete = 2;

originalArray.splice(indexToDelete, 1);

console.log(originalArray); // Output: [1, 2, 4, 5]

In this example, we delete the element at index 2 of the originalArray using the splice() method. The first argument of splice() specifies the index where the deletion should start, and the second argument specifies how many elements should be removed (in this case, 1 because we’re removing only one element).

After executing the splice() method, the originalArray will be updated to [1, 2, 4, 5].

Searching for Item in Array

In JavaScript, you can search for an item in an array using the indexOf() method or the includes() method. Both methods return a boolean value indicating whether the element was found in the array or not.

Here’s an example code snippet in JavaScript to search for an item in an array:

JavaScript
const myArray = ['apple', 'banana', 'orange'];

// Using the indexOf() method to search for an item
const index = myArray.indexOf('banana');
if (index !== -1) {
  console.log('Found at index:', index); // Output: Found at index: 1
} else {
  console.log('Not found');
}

// Using the includes() method to search for an item
const isFound = myArray.includes('orange');
if (isFound) {
  console.log('Found'); // Output: Found
} else {
  console.log('Not found');
}

In this example, we have an array called myArray with three elements. We use the indexOf() method to search for the index of the element 'banana'. The method returns the index of the element if it is found, or -1 if it is not found. We check if the returned index is not -1 and print a message accordingly.

We also use the includes() method to search for the element 'orange'. The method returns true if the element is found in the array, or false if it is not found. We check the boolean value and print a message accordingly.

Note that if you need to perform more complex searches, you may want to use the find() or filter() methods. These methods allow you to search for elements in an array based on a condition or a callback function.

Javascript array last item?

In JavaScript, you can access the last element of an array by using the index length-1. Here’s an example code snippet in JavaScript to access the last element of an array:

JavaScript
const myArray = ['apple', 'banana', 'orange'];

// Accessing the last element of the array
const lastElement = myArray[myArray.length - 1];

console.log(lastElement); // Output: orange

In this example, we have an array called myArray with three elements. We access the last element of the array by using the index myArray.length - 1, which gives us the value 'orange'.

Note that if the array is empty, the myArray.length - 1 expression will evaluate to -1, which will result in an error. To avoid this error, you can check if the array is empty before accessing the last element:

JavaScript
const myArray = [];

if (myArray.length > 0) {
  const lastElement = myArray[myArray.length - 1];
  console.log(lastElement);
} else {
  console.log('Array is empty');
}

In this example, we check if the myArray array is empty before accessing the last element. If the array is empty, we print a message saying that the array is empty.

Share The Tutorial With Your Friends
Twiter
Facebook
LinkedIn
Email
WhatsApp
Skype
Reddit

Check Our Ebook for This Online Course

Advanced topics are covered in this ebook with many practical examples.

Other Recommended Article