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

Asked
Active3 hr before
Viewed126 times

6 Answers

javascriptarraycreate
90%

Semantically it would be better to use an object:,This is true, and should be understood for good reason. However, you can create an array and omit setting the first element, in which case it will still exist (hence the accepted answer being correct) but it'll be marked as empty for you.,A simple solution is to fill the zeroth item:, 16 Why do you want to do that? I ask because when you find yourself trying to circumvent one of the most basic characteristics of a programming language, odds are good there's a better way to do what you're trying to do. – Syntactic May 13 '10 at 12:28

A simple solution is to fill the zeroth item:

var map = [null, 'January', 'February', 'March'];
'First month : ' + map[1];


Semantically it would be better to use an object:

var map = {
   1: 'January',
   2: 'February',
   3: 'March'
};
'First month : ' + map[1];
load more v
88%

Remember, JavaScript Array indexes are 0-based: they start at 0, not 1. This means that the length property will be one more than the highest index stored in the array:,In ES2015, you can use the Array.of static method to create arrays with single element.,JavaScript typed arrays,JavaScript data structures

let arr = new Array(element0, element1, ..., elementN)
let arr = Array(element0, element1, ..., elementN)
let arr = [element0, element1, ..., elementN]
load more v
72%

I know, must be very trivial... Thanks for your help.,By default the indexing of every JavaScript array starts from 0. I want to create an array whose indexing starts from 1 instead.,I faced a situation where I neeeded the array to start from index 1. Here's what I did:,Since this question also pops up for a Google search like "javascript start array at 1" I will give a different answer:

Arrays can be sliced. So you can get a sliced version of the Array like this:

var someArray = [0, 1, 2, 3];

someArray.slice(1);
[1, 2, 3]

someArray.slice(2, 4);
[2, 3]
load more v
65%

There are two syntaxes for creating an empty array:,There is one more syntax to create an array:,That’s because arrays are objects. So both shoppingCart and fruits are the references to the same array.,We can also use alert to show the whole array.

let arr = new Array();
let arr = [];
load more v
75%

Arrays in JavaScript are zero-based, which means the first element index is zero, and the last element is at the array length, minus 1:,An array is an ordered collection of elements. In JavaScript, an array can be created using formal object notation, or it can be initialized using literal notation, as demonstrated in the following code:,Use an associative array to store the elements, using the element identifiers as array index:,To create this in JavaScript using a multidimensional array, use the following code:

var arrObject = new Array("val1", "val2"); // array as object
var arrLiteral = ["val1", "val2"]; // array literal
var arrObject = new Array();
var arrObject = new Array("val1", "val2");
var arrLiteral = ["val1", "val2", "val3"];
someFunction("param1", ["val1", "val2"]);
function chgArray(arr) {
   arr[0] = "surprise!";
}

var newArray = new Array("val1", "val2");
var newLiteral = ["val1", "val2"];

chgArray(newArray);
chgArray(newLiteral);

alert(newArray); // prints surprise!,val2
alert(newLiteral); // prints surprise!,val2
var arrObject = new Array("val1", 34, true); // string, number, boolean
var arrLiteral = [arrObject, "val2", 18, false); // object, string, number, boolean
alert(arrLiteral); // prints out val1,34,true,val2,18,false
var arrObject = new Array();

arrObject[0] = "cat"; // array now has one element
alert(arrObject[0]); // prints cat
var farmAnimals = new Array("cat", "dog", "horse", "pig");
alert(farmAnimals[0]); // print cat
alert(farmAnimals[3]); // print pig
var arrLiteral = ["val1", , "val3"];
var largeCollection = new Array(100); // a new array with 100 undefined elements
load more v
40%

The fill() method fills all the elements of an array from a start index to an end index with a static value. The end index is not included. You can learn more about fill() here.,Here, we have been able to fill all the elements of our created array with 5. You can set any static value for different indexes of the array using the fill() method.,The trick is to call concat() either with an empty array([]) as argument or without any arguments at all:,The trick is to call slice() either with0 as the only argument or without any arguments at all:

While Array.of(5) creates a new array with a single element, 5, and a length property of 1, Array(5) creates a new empty array with 5 empty slots and a length property of 5.

var array1 = Array.of(5); // [5]
var array2 = Array(5); // Array(5) {length: 5}
load more v

Other "javascript-array" queries related to "How to create an array in JavaScript whose indexing starts at 1?"