Learn some JavaScript methods easily but effectively.
Today I’ll explain some highly used methods in JavaScript.
1. Number.isNaN()
Number.isNaN() method shows whether the input value is nan or a number.But it is little confusing because of the global isNaN(). When we will give a number in Number.isNaN() it will return false which same in both.
Example : Console.log(Number.isNaN(10)) // output false
But when we will put a string in input we expect the output will be true because string is not a number.But Unlike global isNaN() it will return false.
Example :
Console.log(Number.isNaN(‘blabla’))//output false
2. Array.splice()
This method is very common and highly used in JavaScript. With the help of this method we can delete an item and add a new item in a specific index in an array. It can take 3 parameters. First the index number, means from what position we want to delete or add item. Secondly the delete count, how many items we want to delete and at last the value we want to put in that index.
Example :
Const days= [sat, mon, tue, wed, thu, fri]days.splice(1, 0, ‘sun’)Console.log(days)//output [sat, sun, mon, tue, wed, thu, fri]
Here we added sun in index 1 without deleting any item.
3. Array.slice()
It has little similarity with splice. It also delete items from array but it returns the deleted values as an new array and unlike splice() it doesn’t effect the root array. It takes 2 parameters. First one is the start and second one is end number(note: not index. It starts from 1 unlike index start from 0).If the end value is not given the default value will be array.length -1.
Example:
Const fruits =[‘mango’, ‘ orange’, ‘cocomelon’, ‘banana’]Const newFruits = fruits.splice(2)Console.log(newFruits)// output [‘cocomelon’, ‘banana’]
4.String.split()
Split is another method of String. It split a string based on a separator and returns a new array.
Example :
Const name = ‘my name is suhrav'Const splitName =name.split(‘ ’)Console.log(splitName)// output [‘my’, ‘name’, ‘is’, ‘suhrav’]
Here we gave a space as separator so that the split method can separate the string based on every white space.
This method can also take another parameter call limit. It specifics a limit how many split items will be in the array.
5.String.substr()
It is also an string method helps us to slice a string and returns new string. It does not change the original string. It is very similar like Array.slice. it takes two parameters, first the start index and second the end number(note: not index).
Example :
Const line = ‘hello world”Const newLine = line.substr(6, 9)Console.log(newLine) // output ‘world'
6.Array.reducer()
This method is little bit confusing for beginners. But lets think in a simple way. First declare a new array.
Const Numbers = [5, 10, 14, 4]
Now if we want to some this array’s numbers what we will do? We will initialize a for loop and we will declare a initial value 0.
Const total = 0
Then we will loop through every number and plus it with our total value.We can do this very easily with reducer(). Reducer() takes a function as an argument. And the function can take 4 parameters. first the initial value, second the current value, third current index and fourth array source.
Now if we wanna do the same thing with reducer we can do it simply :
Const sum=numbers.reducer((total, num)=>total+num)console.log(sum)// output 33
We can also pass the intial value as second parameter in reducer.
7.Array.forEach()
forEach method is very commonly used method in javascript. It’s just like for loop.It is very easier to read.
Example:
const array= [1,2,4,5]const newArray =[]array.forEach(element=>newArray.push(element)Console.log(newArray)//output [1,2,4,5]
8.Array.map
Its is also very useful method in javascript. It is very similar with forEach but unlike for each it returns a new array.
Example :
const array=[1,2,3,4,5]const newArray= array.map(ele=> ele*2)consol.log(newArray)//output [2, 4, 6,8,10]
We use map method when we need something in return. When we Don’t need anything in return we use forEach.
9.parseInt()
Its an very useful method in javascript.we can give any number or string type number as a input to covert it in a integer number.
Example :
console.log(parseInt( ’10.66’))//output 10
10.parseFloat()
This is very similar like parseInt but unlike parseInt() it doesn’t cut the floating values.
Example :
console.log(parseFloat(’10.66’))// output 10.66