Top 9 commonly used javascript functions with example | Akashminds

Akash Chauhan
8 min readOct 10, 2022

--

Functions are one of the most important aspects of JavaScript. Without JavaScript functions, it would be very limited. functions in javascript are used to perform a certain action or task.

they can be written into the code or they can be created using the Function constructor.

Javascript functions or methods in Java can take arguments and return values. Arguments are values that are passed into the function when it is called. The return value is the value that is returned by the function after it has been executed.

Functions can be defined inside of other functions, which are called nested functions. Nested functions have access to the variables and arguments of the outer function.

This allows for some interesting and powerful programming techniques.

As a programming language, JavaScript has a lot of features that make it unique and important. One of these features is the function.

Javscript functions are also important for other framekworks like angular, Angular is a JavaScript framework for building web applications and apps in JavaScript, HTML, and TypeScript.

Angular is used by millions of developers all over the world. It is one of the most popular frameworks for building single-page applications.

In this article, we’ll explore Top 9 commonly used javascript functions with example.

1. JavaScript Filter

The filter function in JavaScript is used to filter out elements from an array based on a certain condition. In other words, the filter function will return a new array with only those elements that satisfy the condition.

For example, if we have an array of numbers and we want to only get the even numbers, we can use the filter function with a condition that checks for even numbers.

Similarly, if we have an array of objects and we want to only get the objects with a certain property value, we can use the filter function with a condition that checks for that property value. There are many other uses for the filter function as well.

JavaScript filter example:

// Array Filter function let numbers = [15, 2, 50, 55, 90, 5, 4, 9, 10]; console.log(numbers.filter(number => number % 2 == 1)); // Filtering odd numbers => result is [15, 55, 5, 9]

2. Javascript foreach

Javascript foreach loop is a handy tool for iterating over arrays in JavaScript. It allows you to execute a certain set of code for each element in an array, without having to write a for loop. we’ll take a look at how the foreach loop works and how you can use it in your own code.

we will discuss what kind of operations we can perform with foreach in javascript. Javascript Foreach is a looping construct that is available in several programming languages, including javascript.

The main purpose of foreach is to allow the programmer to iterate over a collection of data, such as an array or list.

To use the javascript foreach, you first need an array. This can be created using the Array() constructor, or by simply assigning a comma-separated list of values to a variable:

Once you have your array, you can start iterating over it using the javascript foreach loop. The syntax for the foreach loop is as follows:

Javascript foreach example:

<p id="items"></p><script> let text = ""; 
const fruits = ["Apple", "Orange", "Cherry", "Banana"];
fruits.forEach(myFunction);
document.getElementById("items").innerHTML = text; function myFunction(item, index) {
index =index + 1 text += index + ": " + item + "";
} </script>

3. Javascript map

Javascript map function is a built-in method in JavaScript that allows you to process each element in an array.

The map() method in JavaScript is used to transform elements in an array according to a function. The function is executed on each element of the array, with the element being passed as an argument.

Javascript map() method returns a new array containing the transformed elements.

If you have an array of numbers and you want to double them, you can use the map() method with a function that multiplies each number by 2.

In this case, the original array is not modified. Instead, a new array is created with the doubled values:

var newArr = arr.map(num => num * 2);

Let’s take a look at another example of JavaScript map function

const users = [ {firstname : "Abhishek", lastname: "kumar"}, {firstname : "jay", lastname: "sharma"},
{firstname : "rupal", lastname: "sharma"} ];

users.map(getFullName);

function getFullName(item) {
return [item.firstname,item.lastname].join(", ");
}
// Output: Abhishek kumar, jay sharma, rupal sharma,

4. Javascript concat

Javascript string concatenation is the process of joining two or more strings together. The most common way to concatenate strings in javascript is to use the + operator. However, there are other ways to do it as well.

One way to concatenate strings in javascript is by using the += operator. This operator adds the string on the right side of the operator to the string on the left side of the operator. For example:

str1 += str2; // now str1 equals “Hello World”

Another way to concatenate strings in javascript is by using the .concat() method.

Js concat method is used to merge two or more strings together. This is useful if you want to build a single string out of multiple smaller strings.

Javascript concat() method does not change the existing strings, but returns a new string that contains the text of the merged strings.

JavaScript concat example:

const arr1 = ["Abhishek", "rupal"]; 
const arr2 = ["divya", "rahul", "harsh"];
const allUsers = arr1.concat(arr2);
// Output: Abhishek, rupal, divya, rahul, harsh

const arr1 = ["Abhishek", "rupal"];
const arr2 = ["divya", "rahul", "harsh"];
const arr3 = ["kamal", "rohit"];
const allUsers = arr1.concat(arr2, arr3);
// Output: Abhishek, rupal, divya, rahul, harsh, kamal, rohit

5. JavaScript Find

When working with arrays, the find function can be a helpful tool. This function will return the first element in an array that meets the given condition.

If we have an array of numbers and we want to find the first number that is greater than 5, we can use the find function. The javascript find function takes a callback as its first argument.

this callback is passed three arguments: the current element being processed, the index of that element, and the array itself.

The callback should return true if the element meets the condition and false otherwise. In our example, we would return true if the current element is greater than 5.

Javascript find function is not just limited to numbers. It can be used on strings as well.

JavaScript find function example:

const marks = [30, 70, 98, 77];
console.log(marks.find(checkMarks));
function checkMarks(mark) {
return mark > 90;
}
// 98

Another example of javaScript find function:

const fruits = [ { name: "apple", count: 10 }, { name: "banana", count: 18 }, { name: "mango", count: 3 } ]; const findMango = fruits.find(fruit =>fruit.name === "mango"); 
// { name: "mango", count: 3}

6. JavaScript findIndex

When working with arrays, there may be times when you need to find the index of a particular element. This can be done using the Javascript findIndex() method.

Javascript findIndex method returns the index of the first element in the array that satisfies the provided testing function. Otherwise, it returns -1.

findindex javascript method is similar to javascript find function, but it returns the index instead of the value.

The findIndex() function takes two arguments a callback function and an optional object that can be used as the this keyword inside the callback function.

JavaScript findIndex function example:

const marks = [30, 70, 98, 77]; console.log(marks.findIndex(checkMarks)); function checkMarks(mark) {
return mark > 90;
}
// 2

Another example of javaScript findIndex function:

const fruits = [ { name: "apple", count: 10 }, { name: "banana", count: 18 }, { name: "mango", count: 3 } ]; const findMango = fruits.findIndex(fruit =>fruit.name === "mango"); // 2

7. Javascript includes

Javascript includes() is an inbuilt function that checks whether a string contains another string.If the specified string is found, it returns true, otherwise it returns false.

JavaScript includes function is case-sensitive, meaning it will differentiate between uppercase and lowercase letters, meaning it will treat “Java” and “java” as two different strings.

To check if a js string contains another string, simply pass in the string you want to check as the first argument, and the string you’re checking for as the second argument.

For example, let’s check if the string “Hello World” contains the word “world”. Since the search is case-sensitive, it will return false.

const birds = ["Birds", "peacock", "Dove", "Sparrow"]; console.log(birds.includes("Dove")); 
// true

8. JavaScript split

JavaScript split function in JavaScript is a String function, which is used to split a string into an array of substrings, and return the new array.

split(str, separator, limit)

The original string is not modified. The syntax for the split function is:

str — The string to be split.

separator — The character to use as a separator. If omitted, a single space (‘ ‘) is used as the separator.

limit — An integer that specifies the number of splits to be made. If omitted, the string will be split into an array of substrings with no limit.

The separator is a string that defines where the string should be split. The limit is an integer that specifies the maximum number of splits.

If the separator is not specified, the string will be split by whitespace characters. If the limit is not specified, it will default to 0, which means no limit.

Javscript split function return value is an array of substrings. If the string cannot be split, it will return an empty array.

let text = "Hello this is akashminds"; console.log(text.split(" ")); // ["Hello", "this", "is", "akashminds"];let text = "Hello this is akashminds"; console.log(text.split(" ", 3)); // ["akashminds"];

9. JavaScript substr

JavaScript substr function is used to extract a part of the string. It has two parameters: the start position and the length of the substring. The function returns a new string containing the extracted part of the original string.

If the start position is negative, it is counted from the end of the string. If the length parameter is omitted, JavaScript substr extracts all characters from the start position to the end of the string.

javascript substr function can be used to extract a part of a string or to create a new string by concatenating two substrings. It can also be used to find out whether a certain character or substring exists in a given string.

const test = "Hey!, this is Akashminds, have a nice day ahead."; console.log(test.substring(0, 30)); // Hey!, this is Akashminds, have

Summary

As one of the most versatile and widely-used programming languages, JavaScript has a long history and a bright future. In this article, we have explored the top 9 commonly used JavaScript functions with example.

Using functions in JavaScript has a number of advantages.

First, if you have a lot of code that you need to reuse, putting it in a function makes it easy to call again whenever you need it.

Second, using functions can help make your code more readable by breaking it up into smaller pieces.

These are the top 9 commonly used JavaScript functions with example. Mastering these functions will help you write better code and become a more proficient programmer.

Related Articles

Top 9 commoly used rxjs operators .

How much jvascript is required for selenium automation tester.

The 10 essential frontend developer skills that you will need to know in 2023.

What sets Angular apart from other frameworks

Originally published at https://www.akashminds.com.

--

--

Akash Chauhan

Akash Expertises in front-end technologies including javascript, Angular,Rxjs, React.js, HTML, CSS and Jquery.