How do you define an array in javascript?

We have learned that a variable can hold only one value, for example var i = 1, we can assign only one literal value to i. We cannot assign multiple literal values to a variable i. To overcome this problem, JavaScript provides an array.

An array is a special type of variable, which can store multiple values using special syntax. Every value is associated with numeric index starting with 0. The following figure illustrates how an array stores values.

How do you define an array in javascript?
JavaScript Array

Array Initialization

An array in JavaScript can be defined and initialized in two ways, array literal and Array constructor syntax.

Array Literal

Array literal syntax is simple. It takes a list of values separated by a comma and enclosed in square brackets.

var  = [element0, element1, element2,... elementN];

The following example shows how to define and initialize an array using array literal syntax.

var stringArray = ["one", "two", "three"];

var numericArray = [1, 2, 3, 4];

var decimalArray = [1.1, 1.2, 1.3];

var booleanArray = [true, false, false, true];

var mixedArray = [1, "two", "three", 4];

JavaScript array can store multiple element of different data types. It is not required to store value of same data type in an array.

Array Constructor

You can initialize an array with Array constructor syntax using new keyword.

The Array constructor has following three forms.

var arrayName = new Array();

var arrayName = new Array(Number length);

var arrayName = new Array(element1, element2, element3,... elementN);

As you can see in the above syntax, an array can be initialized using new keyword, in the same way as an object.

The following example shows how to define an array using Array constructor syntax.

var stringArray = new Array();
stringArray[0] = "one";
stringArray[1] = "two";
stringArray[2] = "three";
stringArray[3] = "four";

var numericArray = new Array(3);
numericArray[0] = 1;
numericArray[1] = 2;
numericArray[2] = 3;

var mixedArray = new Array(1, "two", 3, "four");

Please note that array can only have numeric index (key). Index cannot be of string or any other data type. The following syntax is incorrect.

var stringArray = new Array();

stringArray["one"] = "one";
stringArray["two"] = "two";
stringArray["three"] = "three";
stringArray["four"] = "four";

Accessing Array Elements

An array elements (values) can be accessed using index (key). Specify an index in square bracket with array name to access the element at particular index. Please note that index of an array starts from zero in JavaScript.

var stringArray = new Array("one", "two", "three", "four");

stringArray[0]; // returns "one"
stringArray[1]; // returns "two"
stringArray[2]; // returns "three"
stringArray[3]; // returns "four"

var numericArray = [1, 2, 3, 4];
numericArray[0]; // returns 1
numericArray[1]; // returns 2
numericArray[2]; // returns 3
numericArray[3]; // returns 4

Array Properties

Array includes "length" property which returns number of elements in the array.

Use for loop to access all the elements of an array using length property.

var stringArray = new Array("one", "two", "three", "four");

for (var i = 0; i < stringArray.length ; i++) 
{
    stringArray[i];
}

  1. An array is a special type of variable that stores multiple values using a special syntax.
  2. An array can be created using array literal or Array constructor syntax.
  3. Array literal syntax: var stringArray = ["one", "two", "three"];
  4. Array constructor syntax:var numericArray = new Array(3);
  5. A single array can store values of different data types.
  6. An array elements (values) can be accessed using zero based index (key). e.g. array[0].
  7. An array index must be numeric.
  8. Array includes length property and various methods to operate on array objects.

Want to check how much you know JavaScript?

How do you define an array?

An array is a collection of elements of the same type placed in contiguous memory locations that can be individually referenced by using an index to a unique identifier. Five values of type int can be declared as an array without having to declare five different variables (each with its own identifier).

What is array in JavaScript with example?

An array is an object that can store multiple values at once. For example, const words = ['hello', 'world', 'welcome']; Here, words is an array.

How do you type an array in JavaScript?

Declaring an array Javascript's elegance owes a lot to its concise literal syntax for the most common building blocks: object, functions and array. A literal is the best way to express an array: var arr = [1, 2, 3, 4];

How do you initialize an array in JavaScript?

You can initialize an array with Array constructor syntax using new keyword. The Array constructor has following three forms. Syntax: var arrayName = new Array(); var arrayName = new Array(Number length); var arrayName = new Array(element1, element2, element3,...