Top 5 Must Known JS Functions

Top 5 Must Known JS Functions

JavaScript has several top-level built-in functions. Let's have a look at

1. Spread Operator

  • The Spread Operator expands an array into its elements. It can also be used for object literals.
    const foodList = ['pizza','fries','samosa'];
    console.log(...foodList);
    // Output : pizza fries samosa
    

2. Filter()

  • The filter() method creates a new array with all elements that pass the test.
    const prices = [25,30,125,34,50];
    prices.filter(function(price){
      return price >= 30;
    });
    // Output : [30,125,34,50]
    

3. Map()

  • The map() method is similar to the filter() method in terms of returning a new array. However, the only difference is that it is used to modify items.
    const productPriceList = [200,350,1500,5000];
    productPriceList.map(function(item){
     return item * 0.75;
    });
    // Output : [150,262.5,1125,3750]
    

4. Reduce()

  • The reduce() method can be used to transform an array into something else, which could be an integer, an object, a chain of promises. Eg : To sum a list of integers. In Short, it "reduces" the whole array into one value.
    const weeklyExpenses = [200,350,1500,5000,450,680,350]
    weeklyExpenses.reduce(function(first,last){
     return first+last;
    });
    // Output : 8530
    

5. Includes()

  • The included() method is used to check if a specific string exists in a collection, and returns true or false.
    const garage = ['BMW','AUDI','VOLVO'];
    const findCar = garage.includes('BMW');
    console.log(findCar);
    // Output : true
    

If you made it till here and find this useful give a follow up (+1) to me.
Checkout my twitter handle : tshrvrm