E2EHIRING Logo
Jobs
Jobs
courses
Courses
mentorship
Mentorship
more
Moredropdown
E2EHIRING Logo
more
Jobs
Jobs
courses
Courses
mentorship
Mentorship
HomeSepratorIconBlogsSepratorIconJavaScript ArraysSepratorIcon

JavaScript Arrays

Han SoloPriyanka Siddalingreddy
calendar22 Jun 2022
poster

JavaScript Arrays


Arrays are special variables that can hold multiple values. JavaScript array is an object that represents a collection of similar types of elements. 

Creating an Array :

Using an array literal is the easiest way to create an array.

Syntax

const array_name = [item1, item2, item3, ...];

As you can see the above syntax values are contained inside the [ ], and separated by a comma (,). 

An example is given below

const fruits = ["mango", "orange", "banana", "grapes" ];

Spaces and line breaks are not important, a declaration can span multiple lines.

const fruits = [ 
"mango",
"orange",
"grapes"
];

You can also create an empty array and then you can provide the elements of an array.

const fruits = [];
fruits [0] = "mango";
fruits [1] = "orange";
fruits [2] = "grapes";

and also using the JavaScript keyword new we can create an array.

const fruits = new Array("mango", "orange", "grapes");

The above two examples do exactly the same so no need to use the new Array().

For simplicity, and readability use the array literal method.

Access an Array Elements

You can access the array elements by using the index number

const fruits = ["mango", "orange", "grapes"];
let fruits = [1]; //Orange
Array indexes start with 0, [0] is the first element, and [1] is the second element.
Changing the Array Elements
The below given example changes the element of a given array.
const fruits = ["mango", "orange", "grapes"];
let fruits[1] = "banana";

Recent Posts

 Introduction to Java Programming: Basics and Fundamentals

Introduction to Java Programming: Basics and Fundamentals

Introduction to Web Technologies: HTML, CSS, and JavaScript

Introduction to Web Technologies: HTML, CSS, and JavaScript

Introduction of JavaScript

Introduction of JavaScript

Java/Kotlin : Interoperability & Benefits

Java/Kotlin : Interoperability & Benefits

Introduction of Bootstrap

Introduction of Bootstrap

copycopycopycopy

Han Solo

Recent Posts

 Introduction to Java Programming: Basics and Fundamentals

Introduction to Java Programming: Basics and Fundamentals

Introduction to Web Technologies: HTML, CSS, and JavaScript

Introduction to Web Technologies: HTML, CSS, and JavaScript

Introduction of JavaScript

Introduction of JavaScript

Java/Kotlin : Interoperability & Benefits

Java/Kotlin : Interoperability & Benefits

Introduction of Bootstrap

Introduction of Bootstrap