#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
JavaScript Fundamentals

Array access methods

Array access methods are the tools you use to read values out of an array without changing it: getting an item at a position, taking a slice, finding where a value sits, or picking the first item that matches a condition. Choosing the right one makes code shorter and clearer than writing a loop each time.

Reading by position

Square brackets with an index give you an item: colors[0] is the first item, because indexes start at zero. The last item sits at colors.length - 1. Reading an index that does not exist returns undefined rather than throwing an error.

The newer at() method accepts negative numbers that count from the end, so colors.at(-1) is the last item and colors.at(-2) the one before it. That is much tidier than repeating the array name inside the brackets.

Copying a portion with slice

slice(start, end) returns a new array containing items from start up to, but not including, end. Leaving out end goes to the last item, and negative values count from the end. Calling slice() with no arguments makes a shallow copy of the whole array.

const scores = [72, 88, 95, 60, 88, 41];

scores[1];              // 88
scores.at(-1);          // 41
scores.slice(1, 3);     // [88, 95]
scores.slice(-2);       // [88, 41]

scores.indexOf(88);     // 1   (first match)
scores.lastIndexOf(88); // 4
scores.indexOf(100);    // -1  (not found)
scores.includes(95);    // true

const students = [
  { name: 'Aman', marks: 72 },
  { name: 'Neha', marks: 95 },
  { name: 'Ravi', marks: 41 }
];

students.find(s => s.marks > 90);        // { name: 'Neha', marks: 95 }
students.findIndex(s => s.name === 'Ravi'); // 2
students.findLast(s => s.marks < 80);     // { name: 'Ravi', marks: 41 }
students.filter(s => s.marks >= 60).length; // 2

Searching for a value or a condition

indexOf() returns the position of the first item that equals the value you pass, using strict equality, or -1 if there is none. lastIndexOf() searches from the end. includes() answers only yes or no and is easier to read when you do not need the position.

When you need to match on a condition rather than an exact value, use find() to get the first matching item or findIndex() to get its position. findLast() and findLastIndex() search backwards. For every match rather than the first one, filter() returns a new array.

All of these methods leave the original array untouched, which matters when the array is shared, for example as React state. Methods such as splice(), pop() and shift() also return items but remove them at the same time, so avoid them when you only want to read. Note too that find() returns the matching element itself, so changing a property on that object changes the object inside the array.

Mistakes with search results

Writing if (scores.indexOf(72)) is a well-known bug. When 72 is at position 0, the result is 0, which is falsy, so the condition fails even though the value exists. And when the value is missing, -1 is truthy. Compare explicitly with !== -1, or simply use includes().

indexOf() also cannot find objects by their contents. students.indexOf({ name: 'Aman', marks: 72 }) returns -1 because a new object is never strictly equal to an existing one. Use findIndex() with a condition instead.

A smaller difference: includes() finds NaN, while indexOf() does not.

Choosing quickly

  • Know the position? Use brackets or at().
  • Need a range? Use slice().
  • Checking whether a value exists? Use includes().
  • Matching on a condition? Use find() or findIndex().

Ready to master real-world web development?

Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.

Explore Course