JavaScript Code


How to get item at given index from array in JavaScript?


Get item at specific index from array

Given an array and an index, write a JavaScript program that gets the item at given index from the given array.

Solution

To get an item at the given index from an array using JavaScript, call at() method on the array object and pass the index as argument.

array1.at(index) method returns the item from array array1 at position index.

Programs

1. Get item from array array1 at position index = 3.

let array1 = [2, 4, 8, 16, 32, 64, 128, 256];
let index = 3;
let item = array1.at(index);
console.log('Item : ' + item);



copyright @2022