Skip to content

Buzzingz

Unleashing the Power of Code

  • HTML
  • CSS
  • JavaScript
  • Toggle search form

JavaScript Array Methods

Posted on April 2, 2023April 2, 2023 By shani No Comments on JavaScript Array Methods

In JavaScript, arrays are used to store multiple values in a single variable. They are used to group related data items and are a commonly used data structure. JavaScript provides many built-in methods for manipulating arrays, making it easy to perform operations like adding or removing elements, sorting, and searching.

In this article, we will explore some of the most commonly used array methods in JavaScript.

push() Method

The push() method adds one or more elements to the end of an array and returns the new length of the array.

Example

let fruits = ["apple", "banana", "orange"];
fruits.push("kiwi", "mango");
console.log(fruits); // Output: ["apple", "banana", "orange", "kiwi", "mango"]

pop() Method

The pop() method removes the last element from an array and returns that element.

Example

let fruits = ["apple", "banana", "orange"];
fruits.
();
console.log(fruits); // Output: ["apple", "banana"]

shift() Method

The shift() method removes the first element from an array and returns that element.

Example

let fruits = ["apple", "banana", "orange"];
fruits.shift();
console.log(fruits); // Output: ["banana", "orange"]

unshift() Method

The unshift() method adds one or more elements to the beginning of an array and returns the new length of the array.

Example

let fruits = ["apple", "banana", "orange"];
fruits.unshift("kiwi", "mango");
console.log(fruits); // Output: ["kiwi", "mango", "apple", "banana", "orange"]

slice() Method

The slice() method returns a new array with a portion of an existing array.

Example

let fruits = ["apple", "banana", "orange", "kiwi", "mango"];
let citrus = fruits.slice(2, 4);
console.log(citrus); // Output: ["orange", "kiwi"]

splice() Method

The splice() method adds/removes elements to/from an array.

Example

let fruits = ["apple", "banana", "orange", "kiwi", "mango"];
fruits.splice(2, 1, "pear");
console.log(fruits); // Output: ["apple", "banana", "pear", "kiwi", "mango"]

concat() Method

The concat() method is used to join two or more arrays and returns a new array.

Example

let fruits1 = ["apple", "banana", "orange"];
let fruits2 = ["kiwi", "mango"];
let fruits = fruits1.concat(fruits2);
console.log(fruits); // Output: ["apple", "banana", "orange", "kiwi", "mango"]

join() Method

The join() method returns a string with all the array elements joined.

Example

let fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo").innerHTML = fruits.join(" * ");
// Output: Banana * Orange * Apple * Mango

map(): This method creates a new array by calling a function on each element of the original array.

filter(): This method creates a new array by filtering out elements that don’t pass a test function.

reduce(): This method applies a function to each element of an array to reduce it to a single value.

sort(): This method sorts the elements of an array in place and returns the sorted array.

reverse(): This method reverses the order of the elements in an array in place and returns the reversed array.

indexOf(): This method returns the first index at which a given element can be found in the array, or -1 if it is not present.

lastIndexOf(): This method returns the last index at which a given element can be found in the array, or -1 if it is not present.

includes(): This method determines whether an array includes a certain element, returning true or false as appropriate.

every(): This method tests whether all elements in the array pass the test implemented by the provided function, returning true or false as appropriate.

some(): This method tests whether at least one element in the array passes the test implemented by the provided function, returning true or false as appropriate.

forEach(): This method executes a provided function once for each array element.

find(): This method returns the value of the first element in the array that satisfies the provided testing function.

findIndex(): This method returns the index of the first element in the array that satisfies the provided testing function, or -1 if no such element is found.

from(): This method creates a new, shallow-copied array instance from an array-like or iterable object.

isArray(): This method determines whether the passed value is an Array.

Javascript

Post navigation

Previous Post: JavaScript Arrays
Next Post: JavaScript Sorting Arrays

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

JavaScript Tutorial

  • JavaScript Tutorial
  • JavaScript Where To
  • JavaScript Output
  • JavaScript Statements
  • JavaScript Syntax
  • JavaScript Comments
  • JavaScript Variables
  • JavaScript Let
  • JavaScript Const
  • JavaScript Operators
  • JavaScript Arithmetic
  • JavaScript Assignment
  • JavaScript Data Types
  • JavaScript Functions
  • JavaScript Objects
  • JavaScript Events
  • JavaScript Strings
  • JavaScript String Methods
  • JavaScript String Search
  • JavaScript Template Literals
  • JavaScript Numbers
  • JavaScript BigInt
  • JavaScript Number Methods
  • JavaScript Number Properties
  • JavaScript Arrays
  • JavaScript Array Methods
  • JavaScript Sorting Arrays
  • JavaScript Array Iteration
  • JavaScript Array Const
  • JavaScript Date Objects
  • JavaScript Date Formats
  • JavaScript Get Date Methods
  • JavaScript Set Date Methods
  • JavaScript Math Object
  • JavaScript Random
  • JavaScript Booleans
  • JavaScript Comparison and Logical Operators
  • JavaScript if, else, and else if
  • JavaScript Switch Statement
  • JavaScript For Loop
  • JavaScript For In
  • JavaScript For Of
  • JavaScript While Loop
  • JavaScript Break and Continue
  • JavaScript Iterables
  • JavaScript Sets
  • JavaScript Maps
  • JavaScript typeof
  • JavaScript Type Conversion
  • JavaScript Bitwise Operations
  • JavaScript Regular Expressions
  • JavaScript Operator Precedence
  • JavaScript Errors
  • JavaScript Scope
  • JavaScript Hoisting: Understanding the Basics
  • JavaScript Use Strict: The Importance of Strict Mode in JavaScript
  • The JavaScript this Keyword
  • JavaScript Arrow Function: A Concise and Comprehensive Guide
  • JavaScript Classes: Understanding the Basics
  • JavaScript Modules
  • JavaScript JSON
  • JavaScript Debugging
  • JavaScript Style Guide
  • JavaScript Best Practices
  • JavaScript Common Mistakes: How to Avoid Them
  • JavaScript Performance: Techniques for Improving Your Code
  • About Us
  • Contact Us
  • Privacy Policy
  • Terms & Conditions

Copyright © 2023 Buzzingz.

Powered by PressBook WordPress theme