Sign up ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

I've been coming across some interview questions that mention within the question "... given an integer x and a zero-indexed array..." Is it necessary to mention zero-indexed array? What languages support non-zero-indexed arrays? Is it possible with javascript?

share|improve this question

put on hold as too broad by gnat, Ixrec, Scant Roger, Snowman, MichaelT Dec 14 at 4:07

There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs.If this question can be reworded to fit the rules in the help center, please edit the question.

2  
re "What languages support non-zero-indexed arrays?": see en.wikipedia.org/wiki/…. Good ol' COBOL's one of them. – senshin Dec 13 at 8:20
    
recommended reading: Why is “Is it possible to…” a poorly worded question? – gnat Dec 13 at 9:47
    
Perl has supported non-0-based array indices for 25 years - and for almost that long, they have been heavily deprecated. – Kilian Foth Dec 13 at 12:13

1 Answer 1

An array in Javascript can be sparse where only some elements exist and the first element that exists does not have to be at index 0. But, the .length property is always measured from index 0.

var array = [];
array[3] = "Hello";
array[9] = "Goodbye";

console.log(array.length);    // 10
console.log(array);           // [3: "Hello", 9: "Goodbye"]
console.log(array[0]);        // undefined

Is it necessary to mention zero-indexed array?

It's not really necessary in Javascript since that is the default expectation, but does confirm that whatever values you are expecting in the array will start at the 0 index.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.