Let’s Dive Into Javascript Build-in Functions

As a web developer, every day we need to work with Javascript. Today in this article I am going to write about 10 Javascript built-in function which will make our life easier as a developer. And every developer should know these 10 built-in functions.

Some Build in Array Method:

  1. Array.map(): The map method allows you to loop through all the elements and allows you to execute the function to all the elements of the calling array and it returns a new array.

parameters: It takes two parameters as an argument. The first one a callback function and the second one is this argument which refers to which value is to use.

callback: It takes a callback function as a first parameter. And this callback function is called for every element of the calling array. When the callback function is executed, the return value added to a new array.

This callback function takes three arguments as a parameter.

currentValue: The current element which is being processed in the array.

index(optional): The current index of the element which is being proccessed in the array.

array(optional): The array map was called.

thisArgument(optional): When executing the callback this argument refers to which value is to use.

Example: Suppose, we have an array like [1, 4, 6, 7, 9]. Now, we want to take all the elements of the array and multiply them by 5. And finally, we want to store them in a new array.

We can solve these types of problems using the map function. Code will be like this.

const numbers = [1, 4, 6, 7, 9];
const newNumbers = numbers.map((item) => item * 5)
console.log(newNumbers)
//output wiill be [5, 20, 30, 35, 45]

2. Array.filter(): This method allows you to loop through all the elements of the array and helps you to filter out the elements which do not match the following condition of the function.

Parameters: It takes two parameters as an argument. First one a callback function and the second one is this argument which refers to which value is to use.

callback: It takes a callback function as a first parameter. And this callback function is called for every element of the calling array. When the callback function is executed, the return value-added to the new array.

This callback function takes three arguments as a parameter.

currentValue: The current element which is being processed in the array.

index(optional): The current index of the element which is being proccessed in the array.

array(optional): The array filter was called.

thisArgument(optional): When executing the callback this argument refers to which value is to use.

Example: Suppose, we have an array of some numbers. Now we want to make an array where the number will be less than 10. We can solve these types of problems using the filter method.

const numbers = [2, 5, 34, 56, 12, 9, 8, 5]
const numberLess10 = numbers.filter((item) => item < 10})
console.log(numberLess10)
//output will be - [2, 5, 9, 8, 5]

3. Array.reduce(): This method allows you to loop through all the elements of the array and resulting in a single value.

parameters: It takes two parameters as an argument. First one a callback function and the second one is this argument which refers to which value is to use.

callback: It takes a callback function as a first parameter. And this callback function is called for every element of the calling array. When the callback function is executed, the return value added to a new array.

This callback function takes four arguments as a parameter.

accumulator: It is used as an state variable which takes the return value.

currentValue: The current element which is being processed in the array.

index(optional): The current index of the element which is being proccessed in the array.

array(optional): The array map was called.

initialValue(optional): This value will be used as the first argument when the callback executes. If we don't supply any values, the first element of the array will be used as an initial accumulator value and skipped as an initial value.

Example: Suppose, we have an array of some numbers. Now, we want to calculate the sum of all the elements of the array. We can solve these types of problems using reduce method.

const numbers = [3, 6, 8, 6, 12, 7];
const total = numbers.reduce((sum, item) => sum += item, 0)
console.log(total)
// output will be - 42
//if we set the initial value=10, the the output will be 52;

4. Array.find(): This method allows you to loop through all the elements of the array and helps you to find out the first element on the following condition.

parameters: It takes two parameters as an argument. First one a callback function and the second one is this argument which refers to which value is to use.

callback: It takes a callback function as a first parameter. And this callback function is called for every element of the calling array. When the callback function is executed, the return value added to a new array.

This callback function takes three arguments as a parameter.

currentValue: The current element which is being processed in the array.

index(optional): The current index of the element which is being proccessed in the array.

array(optional): The array map was called.

thisArgument(optional): When executing the callback this argument refers to which value is to use.

Example: Suppose, we have an array of some numbers. Now we want to find a specific number from the array. We can solve these types of problems using the find method.

const numbers = [1, 5, 7, 9, 33, 67, 55, 90];
const findNumber = numbers.find(item => item === 33)
console.log(findNumber)
//output will be- 33

5. Array.forEach(): This method helps you execute a function of all the elements of the array. It’s an alternative of for loop.

parameters: It takes two parameters as an argument. First one a callback function and the second one is this argument which refers to which value is to use.

callback: It takes a callback function as a first parameter. And this callback function is called for every element of the calling array. When the callback function is executed, the return value added to a new array.

This callback function takes three arguments as a parameter.

currentValue: The current element which is being processed in the array.

index(optional): The current index of the element which is being proccessed in the array.

array(optional): The array map was called.

thisArgument(optional): When executing the callback this argument refers to which value is to use.

Example: Suppose, we have an array of some numbers. Now, we want to multiply all the elements of the array with 10 and s. We can solve these problems using forEach method.

const numbers = [1, 4, 7, 9, 5];
const newNumbersArray = numbers.forEach(item => item * 10)
console.log(newNumbersArray)
//output will be- [10, 40, 70, 90, 50]

Some Build in String Method:

6. String.chatAt(): It returns the character of a specific index in a string.

Parameter: It takes one parameter as an argument which is 0 to (string.length-1). If an index value is not provided, this method takes a default value to 0. So, it will return the first character by default.

If the index value > (string.length-1), it will return empty string(“ ”);

Example:

const str = 'Hello I am learning javascript';
console.log(str.charAt()); //return 'H'
console.log(str.charAt(0)); //return 'H'
consol.log(str.charAt(3)); //retunr 'l'
console.log(str.charAt(33)); //return ''

7. String.slice(): This method extracts a part of a string and returns the extracted part without change the original string.

Syntax: string.slice(statIndex, endIndex);

Parameter: It takes two arguments as a parameter. The start index refers to where the extracts start and the end index refers to where to the end of the extracts.

startIndex: If the startIndex value is negative, it is treated as (str.length + startIndex). If the startIndex value is not a number, it treated as 0. If the startIndex > (str.length), it returned an empty string.

endIndex(optional): If this value is equal to startIndex, it returns an empty string. So, the endIndex value needs to be greater than 0.

Example:

const str1 = 'Hello World';
const str2 = str.slice(0,0);
const str3 = str.slice(2,5);
const str4 = str.slice(-3);
console.log(str2); // output- ''
console.log(str3); //output- 'llo'
console.log(str4); //output-'rld'

8. String.trim(): It removes white spaces from both sides of the string.

Example:

const myName = '  Rakib ';
console.log(myName.trim()); // output- 'Rakib'

Some Built-in Number Method:

9. Number.isNaN(): This method returns true if the passed value is true, otherwise returns false.

Parameter: It takes a value as a parameter.

Example:

console.log(Number.isNaN(NaN)); //true
console.log(Number.isNaN('NaN')); //false
console.log(Number.isNaN(Number.NaN)); //true
console.log(Number.isNaN(12)); //false

Some Built-in Math Method:

10. Math.abs(): This method returns the absolute value of a number.

Example:

console.log(Math.abs(-4)); //output - 4
console.log(Math.abs(2)); // output - 2

Web Developer (MERN Stack)