JavaScript Array Methods()...5

JavaScript Array Methods()...5

·

5 min read

In this article, we are going to learn about following array methods:

  • .map(),
  • .pop(),
  • .shift()
  • .push(),
  • .unshift()

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

In this article, we're going to learn the basics of some array methods in JavaScript(syntax, purpose, as well as example snippets),

If you are new here and want to know some more array methods, I have a series on JavaScript Array Methods(), you can check it out here

Let's get started with array methods-

.map()

  • map() method calls a function for each element in the array and returns a value performing some actions on the elements,

  • It returns a new array that contains the values returned from the function called,

  • Just like some other array methods it also does not calls the function for empty array elements and

  • It does not alter the original array elements,

Syntax

arrayName.map(function(currentElementValue, currentIndex, arrayObject), thisValue)
  1. arrayName - the array to apply the method on,

  2. function(currentElementValue, currentIndex, arrayObject): This is the condition function passed as a parameter to the filter method and it requires the following things:

    • currentElementValue: the 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,
  3. 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.

Example Snippet

var numbers = [1, 2, 3, 4];
numbers.map((num) => num + 2);
// returns [3, 4, 5, 6]

.pop()

  • This method removes an element from the end of an array and returns the value of that element,

  • Requires no parameters,

  • By removing the element from the array changes the length of the array by 1,

  • Syntax

arrayName.pop();

Example Snippet

let numbers= [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
numbers.pop();
//  returns 9

.shift()

  • This method removes an element from the beginning of an array and returns the value of that element,

  • Requires no parameters,

  • By removing the element from the array changes the length of the array by 1,

  • Syntax

arrayName.shift();

Example Snippet

let numbers= [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
numbers.shift();
//  returns 0

.push()

  • This method allows us to add one or more elements at the end of an array and returns the updated length of the array,

  • By adding the element to the array changes the length of the array,

  • Syntax

arrayName.push(valuesToAdd);
  1. arrayName - the array to apply the method on,

  2. valuesToAdd - items to be pushed(added) to the end of the array separated by comma(,),

Example Snippet

let numbers= [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
numbers.push(10, 11, 12);
//  returns 13

.unshift()

  • This method is used to add one or more elements at the beginning of an array and returns the updated length of the array,

  • By adding the elements to the array changes the length of the array,

  • Syntax

arrayName.push(valuesToAdd);
  1. arrayName - the array to apply the method on,

  2. valuesToAdd - items to be added to the beginning of the array separated by comma(,),

Example Snippet

let numbers= [4, 5, 6, 7, 8, 9];
numbers.unshift(0, 1, 2, 3);
//  returns 10
  • And so far we have covered more than half of the array methods, more of them are coming soon, stay tuned, This was article #5 as a part of the series JavascriptArray Methods().