Other way to create array part 2

JavaScript Tutorial
Core Objects
Other Ways to Create Arrays
40
2/2
         

Creating Arrays



JavaScript arrays are dynamic, so you can declare an array and not pass any arguments with the Array() constructor. You can then add the elements dynamically.
var courses = new Array();
courses[0] = "HTML";
courses[1] = "CSS";
courses[2] = "JS";
courses[3] = "C++";
Try It Yourself

You can add as many elements as you need to.

Array Literal



For greater simplicity, readability, and execution speed, you can also declare arrays using the array literal syntax.
var courses = ["HTML", "CSS", "JS"]; Try It Yourself

This results in the same array as the one created with the new Array() syntax. 
You can access and modify the elements of the array using the index number, as you did before.
The array literal syntax is the recommended way to declare arrays.

Comments

Popular posts from this blog

String Operators

Introducing object part 3

Associative Arrays part 2