0% found this document useful (0 votes)
7 views

Arrays in Javascript

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Arrays in Javascript

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

ARRAYS IN

JAVASCRIPT
Creating Array in Javascript

An array is an object that can store multiple values at once. For example,

const words = ['hello', 'world', 'welcome'];

The easiest way to create an array is by using an array literal []. For example,

const array1 = ["eat", "sleep"];


Accessing Array Elements

You can access elements of an array using indices (0, 1, 2 …). For example,

const myArray = ['h', 'e', 'l', 'l', 'o'];

// first element
console.log(myArray[0]); // "h"
// second element
console.log(myArray[1]); // "e"
Array Properties

Array in Javascript can be of mixed data types. For Example

const newData = ['work', 'exercise', 1, true];

You can find the length of an element (the number of elements in an array)
using the length property. For example,

const newData = ['work', 'exercise', 1, true];


const length = newData.length;
Common Array Methods

indexOf() searches an element of an array and returns its position

includes() checks if an array contains a specified element

push() aads a new element to the end of an array and returns the new length of an array

unshift() adds a new element to the beginning of an array and returns the new length of an array

pop() removes the last element of an array and returns the removed element

shift() removes the first element of an array and returns the removed element

sort() sorts the elements alphabetically in strings and in ascending order

slice() selects the part of an array and returns the new array

You might also like