HomeAbout MeContact

How to write an function that accept an array and return a new array with values doubled.

By Gopal Baskota
Published in JavaScript
January 04, 2021
1 min read

We can create a new array with doubled values by using forEach method .

const  doubleValues = (arr) => {
    let newArr = [];
    arr.forEach(function(val) {
      newArr.push(val * 2);
    });
    return newArr;
  }

code Explanation :

  1. In first line we create an arrow function that accepts array a parameter .
  2. In second line we initialize the empty array, in which we will be pushing the doubled values .
  3. In third line we loop through all the values of array using forEach method .
  4. In fourth line we multiply the values of passed array by 2 and passed it into the new array that we initialize before .

Note: Click Here to Learn More about For loops


Tags

javascriptforEach
Previous Article
The every() Array Methods in JavaScript and how to use it .

Gopal Baskota

Full stack Web Developer

Topics

JavaScript
NodeJs
React Js

Related Posts

javascript algorithms - Insertion sort Explained using Javascript .
January 11, 2021
1 min
© 2021, All Rights Reserved.

Quick Links

Advertise with usAbout UsContact Us

Social Media