Master Arrays in JavaScript

Master Arrays in JavaScript

The Ultimate Guide for All Array Properties and Methods in JavaScript

INTRODUCTION

Movie production W.I.P. | Motion design animation, Motion graphics  animation, Animation design

Arrays are an essential part of any programming language, and JavaScript is no exception. They allow us to store and manipulate multiple values in a single structure, making it easy to work with large amounts of data. In this blog, we will explore the various methods available in JavaScript for creating and manipulating arrays, including common tasks such as adding, removing, and sorting elements.

Whether you are a beginner or an experienced developer, I hope that this blog will provide you with a deeper understanding of arrays and how to use them effectively in your code. So without further ado, let's dive in and start learning about arrays in JavaScript!

Cantagiovani 2021 GIFs - Find & Share on GIPHY


What is an Array?

In JavaScript, an array is a collection of values that are stored in a single, ordered structure. Each value in an array is called an element, and elements can be of any data type, including numbers, strings, booleans, and even other arrays.

Here is an example of an array in JavaScript:

const colors = [ 'red', 'blue' , 'white' , 'black' , 'yellow' ]

In this example, colors is an array of five elements: 'red', 'blue', 'white', 'black', and 'yellow'. Arrays are zero-indexed in JavaScript, meaning that the first element of the array is at index 0, the second element is at index 1, and so on. So in this example, 'red' is at index 0, 'blue' is at index 1, and so on.


How can we access array elements?

We can access individual elements of an array using square brackets, like this:

const colors = [ 'red', 'blue' , 'white' , 'black' , 'yellow' ]
console.log(colors[0]) // output: red
console.log(colors[4]) // output: yellow

Array Manipulation Methods

JavaScript provides a wide range of methods to manipulate the array and its elements. These methods are an essential part of JavaScript, as they provide a convenient way to perform common tasks such as adding, removing, and sorting elements in an array. In JavaScript, there are several built-in array manipulation methods that you can use to perform a wide range of operations on arrays.Some examples include push() , pull() etc

By using these array manipulation methods, you can quickly and easily manipulate the elements of an array to meet your specific needs. Whether you are working with small or large amounts of data, these methods provide a powerful and efficient way to manipulate arrays in JavaScript.


Array Properties and Methods

1| length

In JavaScript, the length property of an array is used to get the number of elements in the array.

Here is an example of how you can use the length property:

   const colors = [ 'red', 'blue' , 'white' , 'black' , 'yellow' ]
   console.log(colors.length) // output : 5

2| at( )

In JavaScript, the at ( ) method of an array takes an integer value and returns the item at that index, allowing for positive and negative integers. Negative integers count back from the last item in the array.

Here is an example of how you can use the at() method:

  const colors = [ 'red', 'blue' , 'white' , 'black' , 'yellow' ]
  console.log(colors.at(2)) //  output : white
  console.log(colors.at(-1)) // output : yellow

3| concat ( )

In JavaScript, the concat() method is used to merge two or more arrays. This method does not change the existing arrays but instead returns a new array.

Here is an example of how you can use the concat() method:

  const colors = [ 'red', 'blue' , 'white' , 'black' , 'yellow' ];
  const mixedColor = ['orangered' , 'yelloworange','redpink'];
  const newArray = colors.concat(mixedColor);
 // output : [ 'red', 'blue' , 'white' , 'black' , 'yellow','orangered',            
 //  'yelloworange','redpink']
  console.log(colors)
 // [ 'red', 'blue' , 'white' , 'black' , 'yellow' ] 
 // (Does not change the existing array)

4| filter ( )

In JavaScript, the filter() method in JavaScript is used to create a new array from an existing array, consisting of only the elements of the original array that pass a certain test implemented by a callback function.

Here is an example of how you can use the filter() method:

 const colors = [ 'red', 'blue' , 'white' , 'black' , 'yellow' ];
 const newColors=colors.filter(word=>word.length>3)
 // output: [ 'blue', 'white' , 'black' , 'yellow' ]

5| find( )

In JavaScript, the find() method in JavaScript is used to return the value of the first element in an array that satisfies the provided testing function.If no values satisfy the testing function, undefined is returned.

Here is an example of how you can use the find( ) method:

 const colors = [ 'red', 'blue' , 'white' , 'black' , 'yellow' ];
 const required = colors.find(color=>color.length>3)
 // output : 'blue'

6| findIndex( )

In JavaScript, the findIndex() method returns the index of the first element in an array that satisfies the provided testing function. It simply returns -1 if no element satisfies the given condition.

Here is an example of how you can use the findIndex( ) method:

const colors = [ 'red', 'blue' , 'white' , 'black' , 'yellow' ];
const findIndex=colors.findIndex(color=>color.length>3)
// output : 1

7| findLast( )

In JavaScript, the findLast() method iterates the array in reverse order and returns the value of the first element that satisfies the provided testing function. It simply returns -1 if no element satisfies the given condition.

Here is an example of how you can use the findLast( ) method:

const colors = [ 'red', 'blue' , 'white' , 'black' , 'yellow' ];
const findIndex=colors.findLast(color=>color.length>3)
// output : 'yellow'

8| findLastIndex( )

In JavaScript, the findLastIndex() method iterates the array in reverse order and returns the index of the first element that satisfies the provided testing function.It simply returns -1 if no element satisfies the given condition.

Here is an example of how you can use the findLastIndex( ) method:

const colors = [ 'red', 'blue' , 'white' , 'black' , 'yellow' ];
const findLastIndex=colors.findLast(color=>color.length>3)
// output : 4

9| flat( )

In JavaScript, the flat() method creates a new array with all sub-array elements concatenated into it recursively up to the specified depth.

Here is an example of how you can use the flat( ) method:

const colors = [ 'red', ['blue','orange'] , 'white' , 'black' , 'yellow' ];
console.log(colors.flat())
// ['red', 'blue','orange' , 'white' , 'black' , 'yellow']
const colorsMix = ['red', [['blue','grey'],'orange'] , 'white' , 'black' , 'yellow']
console.log(colorsMix.flat(2))
// ['red', 'blue', 'grey', 'orange', 'white', 'black', 'yellow']

10| flatMap( )

In JavaScript, the flatMap() method returns a new array formed by applying a given callback function to each element of the array and then flattening the result by one level.

Here is an example of how you can use the findMap( ) method:

const nums= [ 1, 2,[3,4],5,6,[7,8],9]
console.log(nums.flatMap(num=>num*0))
// [0, 0, 0, 0, 0, 0, 0, 0, 0]

11| forEach( )

In JavaScript, the forEach() method executes a provided function once for each array element.

Here is an example of how you can use the forEach( ) method:

const colors = [ 'red', 'blue' , 'white'];
console.log(colors.forEach(color=>console.log(color)))
// red
// blue
// white

12| includes( )

In JavaScript, the includes() method can be used to search for a value within an array. It returns a boolean value indicating whether the value is found in the array.

Here is an example of how you can use the includes( ) method:

const colors = [ 'red', 'blue' , 'white' , 'black' , 'yellow' ];
console.log(colors.includes('red'));
// output : true

13| indexOf( )

In JavaScript, the indexOf() method is used to search for a specific value in an array and return its index, or position, within the array. If the value is not found in the array, indexOf() returns -1.

Here is an example of how you can use the indexOf( )method:

const colors = [ 'red', 'blue' , 'white' , 'black' , 'yellow' ];
console.log(colors.indexOf('blue'))
// output: 1

14| Array.isArray( )

In JavaScript, the Array.isArray( ) method is a static method that determines whether the passed value is an array or not.

Here is an example of how you can use the Array.isArray( ) method:

const colors = [ 'red', 'blue' , 'white' , 'black' , 'yellow' ];
console.log(Array.isArray(colors));
// output: true

Taking-in-deep-breath GIFs - Get the best GIF on GIPHY

15| join( )

In JavaScript, the join() method is an array method that creates a new string by concatenating all elements in an array, separated by a specified separator string.

Here is an example of how you can use the join( ) method:

const colors = [ 'red', 'blue' , 'white' , 'black' , 'yellow' ];
console.log(colors.join('-'));
// 'red-blue-white-black-yellow'

16| map( )

In JavaScript, the map() method creates a new array populated with the results of calling a provided function on every element in the calling array.

Here is an example of how you can use the map( ) method:

const nums = [ 1,2,3,4,5,6,7];
console.log(nums.map(num=>num**2));
// [1, 4, 9, 16, 25, 36, 49]

17| pop( )

In JavaScript, the pop() method removes the last element from an array and returns that element. This method changes the existing array.

Here is an example of how you can use the pop( ) method:

const colors = [ 'red', 'blue' , 'white' , 'black' , 'yellow' ];
console.log(colors.pop());
// yellow
console.log(colors);
//  [ 'red', 'blue' , 'white' , 'black' ]

18| push( )

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

Here is an example of how you can use the push( ) method:

const colors = [ 'red', 'blue' , 'white' , 'black' , 'yellow' ];
console.log(push('pink'));
console.log(colors)
// 6
// ['red', 'blue', 'white', 'black', 'yellow', 'pink']

19| reverse( )

In JavaScript, the reverse() method reverses the order of the elements in an array.

Here is an example of how you can use the reverse( ) method:

const colors = [ 'red', 'blue' , 'white' , 'black' , 'yellow' ];
colors.reverse();
console.log(colors);
//['yellow', 'black', 'white', 'blue', 'red']

20| shift( )

In JavaScript, the shift() method removes the first element from an array and returns that removed element. This method changes the existing array.

Here is an example of how you can use the shift( ) method:

const colors = [ 'red', 'blue' , 'white' , 'black' , 'yellow' ];
console.log(colors.shift());
console.log(colors);
// red
// ['blue', 'white', 'black', 'yellow']

21| slice( )

In JavaScript, the slice() method returns a shallow copy of a portion of an array into a new array object selected from start to end where the start and end (not included )represent the index of items in that array. This method does not change the existing array.

Here is an example of how you can use the slice( ) method:

const colors = [ 'red', 'blue' , 'white' , 'black' , 'yellow' ];
console.log(colors.slice(1,3));
console.log(colors)
// ['blue', 'white']
// ['red', 'blue', 'white', 'black', 'yellow']

22| some( )

In JavaScript, the some() method is a built-in JavaScript function that tests whether at least one element in an array passes the test implemented by a testing function.

Here is an example of how you can use the some( ) method:

const colors = [ 'red', 'blue' , 'white' , 'black' , 'yellow' ];
console.log(colors.some(color=>color.length===3))
// output: true

23| unshift( )

In JavaScript, the unshift() method adds one or more elements to the beginning of an array and returns the new length of the array.This method changes the existing array.

Here is an example of how you can use the unshift( ) method:

const colors = [ 'red', 'blue' , 'white' , 'black' , 'yellow' ];
console.log(colors.unshif('pink'))
console.log(colors)
// 6
// ['pink', 'red', 'blue', 'white', 'black', 'yellow']

24| splice( )

In JavaScript, the splice() method is a built-in JavaScript function that adds and/or removes elements from an array. The splice() method takes three arguments: the index at which to start modifying the array, the number of elements to remove, and the elements to add. It modifies the original array and returns an array containing the removed elements.

Here is an example of how you can use the splice( ) method:

const colors = [ 'red', 'blue' , 'white' , 'black' , 'yellow' ];
colors.splice(2,2,'orange')
console.log(colors)
// ['red', 'blue', 'orange', 'yellow']

25| reduce( )

In JavaScript, the reduce() method is a built-in JavaScript function that applies a function to each element in an array, resulting in a single output value. It is often used to calculate a single value based on all the elements in an array.

The reduce() method takes two arguments: a callback function and an initial value (also called an accumulator). The callback function is called for each element in the array and takes two arguments: an accumulator and the current element. It returns a new accumulator value based on the current element. The initial value of the accumulator is provided as the second argument to reduce().

Here is an example of how you can use the reduce( ) method:

const nums = [1, 2, 3, 4, 5];
const sum = nums.reduce((accumulator, currentValue) => accumulator + currentValue, 0);
console.log(sum);  // Output: 15

26| sort( )

In JavaScript, the sort() method is a built-in JavaScript function that sorts the elements of an array in place and returns the sorted array. It is often used to sort an array of strings alphabetically or an array of numbers numerically, but it can also be used to sort an array of objects based on a specific property of the objects.

By default, the sort() method sorts the elements of an array in ascending order (from smallest to largest for numbers, and from A to Z for strings).

Here is an example of how you can use the sort( ) method:

const colors = [ 'red', 'blue' , 'white' , 'black' , 'yellow' ];
colors.sort()
console.log(colors)
// ['black', 'blue', 'red', 'white', 'yellow']

27| every( )

In JavaScript, the every() method tests whether all elements in the array pass the test implemented by the provided function. It returns a Boolean value.

Here is an example of how you can use the every( ) method:

const arr=[ 1,2,3,5,6,4,78,3,96,4];
console.log(arr.every(num=> num>0));
// true

28| fill ( )

In JavaScript, the fill() method changes all elements in an array to a static value, from a start index to an end index. It returns the modified array.

Here is an example of how you can use the fill( ) method:

const colors = [ 'red', 'blue' , 'white' , 'black' , 'yellow' ];
console.log(colors.fill('brown'))
//  ["brown", "brown", "brown", "brown", "brown"]

Great Work GIFs | Tenor

Thank you so much for taking the time to read my blog on arrays in JavaScript. I hope that you found the information useful and that it helped to deepen your understanding of this important topic. If you have any additional questions or would like to learn more about arrays in JavaScript, please don't hesitate to reach out.

Thank You GIFs - Download on Funimada.com