Cookie Consent by Free Privacy Policy Generator 📌 15 Must-Know Javascript array methods


✅ 15 Must-Know Javascript array methods


💡 Newskategorie: Programmierung
🔗 Quelle: dev.to

Arrays are an essential part of any programming language, and JavaScript is no exception. With arrays, developers can store and manipulate collections of data, including strings, numbers, and even objects. In this article, we will cover 15 must-know JavaScript array methods that everyone should know.

push() – Adds one or more elements to the end of an array.

The push() method adds one or more elements to the end of an array and returns the new length of the array. This method is useful when you need to add elements to an array without specifying the index.

const array = [1, 2, 3];  array.push(4, 5);  console.log(array); // Output: [1, 2, 3, 4, 5]

pop() – Removes and returns the last element from an array.

The pop() method removes and returns the last element from an array. This method is useful when you need to remove an element from the end of an array.

const array = [1, 2, 3];
    const lastElement = array.pop();
    console.log(array); // Output: [1, 2]
    console.log(lastElement); // Output: 3

shift() – Removes and returns the first element from an array

The shift() method removes and returns the first element from an array. This method is useful when you need to remove an element from the beginning of an array.

const array = [1, 2, 3];
    const firstElement = array.shift();
    console.log(array); // Output: [2, 3]
    console.log(firstElement); // Output: 1

unshift() – Adds one or more elements to the beginning of an array

The unshift() method adds one or more elements to the beginning of an array and returns the new length of the array. This method is useful when you need to add elements to an array at the beginning.

const array = [1, 2, 3];
    array.unshift(4, 5);
    console.log(array); // Output: [4, 5, 1, 2, 3]

splice() – Adds or removes elements from an array at a specified index

The splice() method adds and removes elements from an array at a specified index. This method can be used to add or remove elements from anywhere in the array.

const array = [1, 2, 3, 4];
    array.splice(1, 2, 5, 6);
    console.log(array); // Output: [1, 5, 6, 4]

slice() – Returns a copy of a portion of an array specified by its starting and ending indexes

The slice() method returns a copy of a portion of an array specified by its starting and ending indexes. This method can be used to create a new array that contains a subset of the original array.

const array = [1, 2, 3, 4];
    const newArray = array.slice(1, 3);
    console.log(newArray); // Output: [2, 3]

concat() – Combines two or more arrays and returns a new array

The concat() method combines two or more arrays and returns a new array. This method can be used to join arrays together without modifying the original arrays.

const array1 = [1, 2];
    const array2 = [3, 4];
    const newArray = array1.concat(array2);
    console.log(newArray); // Output: [1, 2, 3, 4]

join() – Joins all elements of an array into a string

The join() method joins all elements of an array into a string, using a specified separator. This method can be used to create a string from an array.

const array = [1, 2, 3];  const string = array.join("-");  console.log(string); // Output: "1-2-3"

indexOf() – Returns the index of the first occurrence of a specified element in an array

The indexOf() method returns the index of the first occurrence of a specified element in an array. If the element is not found, this method returns -1.

const array = [1, 2, 3];  const index = array.indexOf(2);  console.log(index); // Output: 1

lastIndexOf() – Returns the index of the last occurrence of a specified element in an array

The lastIndexOf() method returns the index of the last occurrence of a specified element in the array. If the element is not found, this method returns -1.

const array = [1, 2, 3, 2];  const index = array.lastIndexOf(2);  console.log(index); // Output: 3

forEach() – Executes a provided function once for each element in an array

The forEach() method executes a provided function once for each element in an array. This method can be used to perform an operation on each element of an array.

const array = [1, 2, 3];  array.forEach((element) => {  console.log(element);  });  // Output:  // 1  // 2  // 3

map() – Creates a new array with the results of calling a provided function on every element in the array

The map() method creates a new array with the results of calling a provided function on every element in the array. This method can be used to create a new array based on the original array.

const array = [1, 2, 3];  const newArray = array.map((element) => {  return element * 2;  });  console.log(newArray); // Output: [2, 4, 6]

filter() – Creates a new array with all elements that pass a test specified by a provided function

The filter() method creates a new array with all elements that pass a test specified by a provided function. This method can be used to create a new array based on a condition.

const array = [1, 2, 3];  const newArray = array.filter((element) => {  return element > 1;  });  console.log(newArray); // Output: [2, 3]

reduce() – Executes a provided function on each element of the array and returns a single value

The reduce() method executes a provided function on each element of the array and returns a single value. This method can be used to perform an operation on all elements of an array and return a single value.

const array = [1, 2, 3];  const sum = array.reduce((accumulator, currentValue) => {  return accumulator + currentValue;  }, 0);  console.log(sum); // Output: 6

sort() – Sorts the elements of an array in place

The sort() method sorts the elements of an array in place. This method can be used to sort an array in ascending or descending order.

const array = [3, 2, 1];
array.sort();
console.log(array); // Output: [1, 2, 3]

Conclusion
In this article, we have covered 15 must-know JavaScript array methods that everyone should know. These methods are essential for working with arrays in JavaScript and can greatly simplify your code. By using these methods, you can perform a wide range of operations on arrays, such as adding, removing, sorting, and filtering elements. Whether you’re a beginner or an experienced developer, mastering these array methods will make your life easier and your code more efficient.

Those were the 15 must-know javascript array methods for you. I really hoped that you enjoyed reading this article.

Follow me on socials:

Twitter: https://twitter.com/Hy_piyush
Instagram: https://www.instagram.com/piyush_kesarwani22/
Linkedin: https://www.linkedin.com/in/piyush-kesarwani-809a30219/
GitHub: https://github.com/piyushkesarwani

...

✅ JavaScript Array Tutorial – Array Methods in JS


📈 39.89 Punkte

✅ Learn the JavaScript Array.every() and Array.some() methods


📈 39.89 Punkte

✅ Select Element in Array() to a new Array() JavaScript


📈 29.01 Punkte

✅ JavaScript Array Length – How to Find the Length of an Array in JS


📈 29.01 Punkte

✅ JS Sum of an Array – How to Add the Numbers in a JavaScript Array


📈 29.01 Punkte

✅ JavaScript Sort Array - How to Sort an Array Accurately


📈 29.01 Punkte

✅ Convert Array of Starings to Array of Numbers in JavaScript


📈 29.01 Punkte

✅ 30 Essential Array Methods in JavaScript with Examples


📈 28.64 Punkte

✅ filter vs. find: JavaScript Array Methods


📈 28.64 Punkte

✅ Array Sort Methods in JavaScript.!


📈 28.64 Punkte

✅ HIGHER ORDER ARRAY METHODS IN JAVASCRIPT


📈 28.64 Punkte

✅ JavaScript Array Methods Cheatsheet


📈 28.64 Punkte

✅ JavaScript Array Methods with Practical Examples


📈 28.64 Punkte

✅ Understanding JavaScript Array Methods: forEach, map, filter, reduce, and find


📈 28.64 Punkte

✅ JavaScript Array Methods: Understand By Implementing


📈 28.64 Punkte

✅ Array search methods in JavaScript.!


📈 28.64 Punkte

✅ JavaScript Array methods


📈 28.64 Punkte

✅ 20 JavaScript: Array Methods


📈 28.64 Punkte

✅ Array methods in JavaScript.!


📈 28.64 Punkte

✅ JavaScript Array Methods: Understanding `flatMap`


📈 28.64 Punkte

✅ JavaScript Array Methods: A Comprehensive Guide


📈 28.64 Punkte

✅ Methods of Array in Javascript


📈 28.64 Punkte

✅ 10 JavaScript Array Methods That Will Make You Look Like a Pro


📈 28.64 Punkte

✅ The Javascript higher order array methods with reactjs examples


📈 28.64 Punkte

✅ Ultimate Guide to Mastering JavaScript Array Methods


📈 28.64 Punkte

✅ Unleashing the Power of Customized Array Methods in JavaScript.


📈 28.64 Punkte

✅ Mastering Advanced Array Methods in JavaScript: Unlock the Full Potential of Your Code


📈 28.64 Punkte

✅ JavaScript Array Methods: Map vs Filter vs Redux


📈 28.64 Punkte

✅ 3. Building JavaScript Array Methods from Scratch in 2024 - Easy tutorial for beginners.


📈 28.64 Punkte

✅ All the new javascript array methods you missed in 2023


📈 28.64 Punkte

✅ 2. Building JavaScript Array Methods from Scratch in 2024 - Easy tutorial for beginners.


📈 28.64 Punkte

✅ Summary of Common Javascript Array Methods


📈 28.64 Punkte

✅ Exploring JavaScript Array Methods with Examples


📈 28.64 Punkte

✅ Building JavaScript Array Methods from Scratch in 2024 - Easy tutorial for beginners # 1


📈 28.64 Punkte

✅ JavaScript, useState React Array Handling methods.


📈 28.64 Punkte











matomo

Datei nicht gefunden!