JavaScript Array Methods()...3

JavaScript Array Methods()...3

Methods in this article:

  • .forEach(),
  • .from(),
  • .includes(),
  • .indexOf()

Hellooo and Welcome to article #3 in the series JavaScript Array Methods(),

Another post where we're going to learn the basics of some array methods in JavaScript(syntax, purpose, as well as example snippets),

You can check my previous posts on JavaScript array methods here

So, not wasting much time, let's get straight on to the point

.forEach()

  • forEach method is an alternative to the for loop for arrays,
  • It iterates through the array and performs some function for each element,

Syntax

arrayName.forEach(function(currentElementValue, currentIndex, arrayObject), thisValue)
  1. function(currentElementValue, currentIndex, arrayObject): This is the function passed as a parameterbe performed on the currentElementValue:

    • currentElementValue: value of the element currently in process,
    • currentIndex: (optional)the index of currentElementValue,
    • arrayObject: (optional)the array object this method is applied to or the array object current element belongs to,
  2. thisValue- (optional) A value to be passed to the method as a 'this' value to the function,

    • "undefined" is the default value of thisValue, if you omit this parameter, the value 'undefined' will be passed.
  • It doesn't change the initial array,
  • It doesn't perform the function on empty array elements.

    Example Snippet

    var numbers = [1, 2, 3, 4];
    numbers.forEach( function(num) {
    console.log(num);
    });
    // 1 
    // 2
    // 3
    // 4
    

.from()

  • This method creates an array instance of any existing array(or string),
  • In simple words .from() method is used to copy an existing array's elements into a new array or if applied on a string it creates a new array taking each letter of any string as an individual array element,

Syntax

Array.from(ArrayObject, mapFunction, thisValue)
  1. ArrayObject: This is the array or object, new instances to be created from,
  2. mapFunction: (optional) This function can be executed on each element to modify the elements as desired, before putting the element into the new array,
  3. thisValue- (optional) A value to be passed to the method as this value to the function,

    • 'undefined' is the default value of this value, if you omit this parameter, the value 'undefined' will be passed.
  • It executes the mapFunction for empty array elements,

Example Snippet

let colors = ["black", "blue", "yellow", "purple"]
let  colorsCopied = Array.from(colors);
console.log(colorsCopied);
//  ["black", "blue", "yellow", "purple"]

includes()

  • This method is used to check the presence of any element inside an array,
  • It takes the value(element) as a parameter and iterates through the elements of an array, and checks that if the passed value exists in the array or not,
  • If it finds the element inside the array it returns true otherwise false

Syntax

arrayName.includes(elementToFind, startIndex)
  1. elementToFind: The element to find in the array,
  2. starIndex: Index where to start searching the element inside the array(default is 0),

Example Snippet

let colors = ["black", "blue", "yellow", "purple"]
colors.includes("blue");
// true

.indexOf()

  • This method helps you find the index position of any element inside an array,
  • It takes the element(to get the index of) as a parameter and iterates over the elements inside an array(to find the index in) and checks if that element exists inside or not,

  • If it finds the element inside the array, it returns the index of that element otherwise it returns -1

Syntax

arrayName.indexOf(elementToFindIndexOf, startIndex)
  1. elementToFindIndexOf: The element you want to find the index of,
  2. startIndex: (Optional) (Default is 0)You can set where to start finding the index using this parameter,

Example Snippet

let colors = ["black", "blue", "yellow", "purple"]
colors.indexOf("purple");
// 3

That's it for now,

Thanks for reading so far,

see you in the next one(coming soon),

till then stay safe stay home stay negative take care

and if you haven't checked out my previous articles, you can check them out here