Everything You Need to Know About JavaScript Array Methods

Last updated on Apr 26,2024 1.3K Views
A Data Science Enthusiast with in-hand skills in programming languages such as... A Data Science Enthusiast with in-hand skills in programming languages such as Java & Python.

Everything You Need to Know About JavaScript Array Methods

edureka.co

Web development or web programming gave birth to dynamic web applications. With the rise of the web, JavaScript has become one of the most important languages in today’s world. This JavaScript Array article will take you to the depths of array methods in JavaScript in the following sequence:

 

Introduction to JavaScript

JavaScript is a high level, interpreted, programming language used to make web pages more interactive. It is a very powerful client-side scripting language which makes your webpage more lively and interactive.

It is a programming language that helps you to implement a complex and beautiful design on web pages. If you want your web page to look alive and do a lot more than just gawk at you, JavaScript is a must.

 

Fundamentals of JavaScript

If you are new to the language, you need to know some of the fundamentals of JavaScript that will help you start writing your code. The basics include:

You can check out the JavaScript Tutorial to get into the depth of these basic concepts and fundamentals of JavaScript. In this JavaScript Array article, we will focus on the different array methods used to define a list of items.

Related Learning: JavaScript Interview Questions

JavaScript Array

An array is a data structure that contains a list of elements which store multiple values under a single variable.

To declare an array in JavaScript use the ‘let’ keyword with square brackets and enclose all the elements within them. The syntax is as follows:


let ListItems=[];
ListItems=['shoes','watch','bag'];

You can also declare it as:


let ListItems=['shoes','watch','bag'];

 

Difference between Array and Objects

JavaScript variables can be objects. Arrays are considered to be special kinds of objects. Because of this, you can have variables of different types in the same Array.


myArray[0] = Date.now;
myArray[1] = myFunction;
myArray[2] = myItems;

In JavaScript, arrays use numbered indexes. Whereas, objects are used as named indexes.

Transform your design skills with our UI UX Design Course.

JavaScript Array Methods

The purpose of using an array is to store multiple values in a single entity of a declared variable. Arrays are used when we want to access elements in an orderly fashion using a single variable. One can store strings, boolean and numbers in a single array.

 

There are different JavaScript array methods in order to perform various tasks such as:

Example:


let listItems = ['bag','shoes','dress'];
console.log(listItems.push('watch'));

Output:

4

Push() doest not return the value that has been added to the array. It only returns the new length of the array.

Example:

let listItems = ['bag','shoes','dress'];
console.log(listItems.pop());

Output:

dress

Pop() returns the value that has been removed and not the array length like Push().

Example:


let listItems = ['bag','shoes','dress'];
console.log(listItems.shift());

Output:

bag

Shift() works same as pop() but it returns the first element of the array instead of the last one.

Example: 


let listItems = ['bag','shoes','dress','watch'];
console.log(listItems.unshift('phone'));

Output:

5

Unshift() will add the new element into the array and return the length of the new array.

Example:


let arr1 = ['red','blue','green'];
let arr2 = ['colors','spraypaint', 'brush'];
let newArr = arr1.concat(arr2);
console.log(newArr);

Output:

 

 

Example:


let colors = ['red','blue','green'];
console.log(colors.toString());

Output:

red,blue,green

Example:


let colors = ['red','blue','green'];
console.log(colors.join("+"));

Output:

red+blue+green

Example:


let fruits = ['mango','apple','grapes'];
console.log(fruits.reverse());

Output:

 

 

Example:


let fruits = ['mango','apple','grapes'];
console.log(fruits.sort());

Output:

 

 

Example:


let colors = ['red','blue','green','yellow','orange'];
console.log(colors.slice(1,3));

Output:

 

 

These were some of the most commonly used JavaScript array methods. With this, we have come to the end of our article. I hope you understood how array methods are used in JavaScript.

Now that you know about JavaScript Array Methods, check out the Web Development Certification Training by Edureka. Web Development Certification Training will help you Learn how to create impressive websites using HTML5, CSS3, Twitter Bootstrap 3, jQuery and Google APIs and deploy it to Amazon Simple Storage Service(S3). 

Check out the Angular Course Online by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. Angular is a JavaScript framework that is used to create scalable, enterprise, and performance client-side web applications. With Angular framework adoption being high, performance management of the application is community-driven indirectly driving better job opportunities.

If you want to get trained in React and wish to develop interesting UI’s on your own, then check out the React JS Course by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe.

Got a question for us? Please mention it in the comments section of “JavaScript Array” and we will get back to you.

BROWSE COURSES