Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

155
Views
JavaScript arrays and understanding arr[arr.length]

What is meant by arr[arr.length]. I mean what it really means or what it really does, I cannot understand the logic of how it works. Can anybody explain?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

This statement gets the index of the last index of the array plus one. It is used to append items to an array. Equivalent to array.push().

var fruits = ["apple", "orange", "banana", "pear"];
// appends grapes to the array
fruits[fruits.length] = "grapes";
console.log(fruits);

For further reading, check out W3School's page on array methods.

about 4 years ago · Juan Pablo Isaza Report

0

arr.length is the length of the array.

arr[arr.length] is accessing by index the array length (out of bounds as length starts at 1, not index 0).

example:

const test = [1,2,3]
test[test.length]
undefined
test[test.length-1]
3
about 4 years ago · Juan Pablo Isaza Report

0

arr.length means the length of the array that spans from index 0 to arr.length - 1.

For instance if you got arr = ["a", "b", "c"], arr.length is 3.

arr[0] is "a", arr[1] is "b" and arr[2] is "c".

arr[3] does not exist, yet. but if you do arr[arr.length] = "d", you obtain ["a", "b", "c", "d"].

This is an odd construction and usually, one should simply write arr.push("d").

const arr = ["a", "b", "c"];
console.log(arr.length, arr[0], arr[1], arr[2], arr[3]);
arr[arr.length] = "d";
console.log(arr);
arr.push("e");
console.log(arr);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!