JavaScript One Dimensional Array

You are Here:

JavaScript One Dimensional Array

JavaScript one Dimensional Array is used to hold multiple values of different data types (if necessary) in a single variable.

Note: In JavaScript, it is not necessary to declare the size of an array.

Create using Array Literal

Using an array literal is the easiest way to create a JavaScript Array.

Note: This is the most common way of creating an array in JavaScript.

Example

HTML Online Editor
<!DOCTYPE html> <html> <body> <script> var myCar = ["Aston Martin", "Jaguar", "Maserati"]; document.write("I like " +myCar[0]); </script> </body> </html>

Syntax

The following are the syntax to create an array.

// Using array literal var array_name= [item1, item2, ..., itemN]; // Using new Keyword var array_name = new Array(item1, item2, ..., itemN);

Create Using new Keyword

Like string and number, An array can also be created using the new keyword.

Example

HTML Online Editor
<!DOCTYPE html> <html> <body> <script> var myCar = new Array("Aston Martin", "Jaguar", "Maserati"); document.write("I love "+myCar[1]); </script> </body> </html>

Initialize the Array Using Index Value

Example

HTML Online Editor
<!DOCTYPE html> <html> <body> <script> var myCar = []; myCar[0]="Aston Martin"; myCar[1]="Jaguar"; myCar[2]="Maserati"; document.write("I too like " +myCar[2]); </script> </body> </html>

Access Full Array

Example

HTML Online Editor
<!DOCTYPE html> <html> <body> <script> var myCar = ["Aston Martin", "Jaguar", "Maserati"]; document.write("I have " +myCar); </script> </body> </html>

Array with Different Types of data

In the following example, we will have different types of data in an array.

Example

HTML Online Editor
<!DOCTYPE html> <html> <body> <script> var myCar = ["Aston Martin", "Jaguar", {car:"Maserati", color:"red"}]; document.write("I like " +myCar[2].color); </script> </body> </html>

Reminder

Hi Developers, we almost covered 97% of JavaScript Tutorials with examples for quick and easy learning.

We are working to cover every Single Concept in JavaScript.

Please do google search for:

Join Our Channel

Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.

This channel is primarily useful for Full Stack Web Developer.

Share this Page

Meet the Author