Skip to content
Advertisement

How to create an array in JavaScript whose indexing starts at 1?

By default the indexing of every JavaScript array starts from 0. I want to create an array whose indexing starts from 1 instead.

I know, must be very trivial… Thanks for your help.

Advertisement

Answer

It isn’t trivial. It’s impossible. The best you could do is create an object using numeric properties starting at 1 but that’s not the same thing.

Why exactly do you want it to start at 1? Either:

  • Start at 0 and adjust your indices as necessary; or

  • Start at 0 and just ignore index 0 (ie only use indices 1 and up).

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement