10 useful JavaScript method every JavaScript developer should know
As a JavaScript developer, You should know these JavaScript methods.
1.parseInt(): This method converts a string to an integer.
Example:
var num = ‘11’;console.log(typeof num); //stringnum = parseInt(num);console.log(typeof num); //number
2.includes(): This method returns true or false. If a string or array contains the characters of a specified string or contains a specified element of an array, then this method returns true, and if not, then returns false.
Example:
var name = [“Limon”, “Mahbub”, “Alam”, “Tanvir”];var isInclude = name.includes(“Alam”);console.log(isInclude); // true
3.map(): This method creates a new array by calling a function. This function will then be executed on each of the array’s elements.
Example:
const numbers = [2, 4, 6, 8];const numbersMap = numbers.map(num => num * 2);console.log(numbersMap); //4, 8, 12, 16
4. forEach(): This method used to loop through an array by passes a callback function.
Example:
const fruits = [“Banana”, “Mango”, “Orange”, “Apple”, “Dragon Fruit”];fruits.forEach(fruit => console.log(fruit));/*Output:BananaMangoOrangeAppleDragon Fruit*/
5. concat(): This method joins multiple arrays by returning a new array
Example:
const num1 = [1,2,3];const num2 = [4,5,6];const num3 = num1.concat(num2);console.log(num3); //1, 2, 3, 4, 5, 6
6.join(): This method converts an array to a string by concatenating all of the array’s elements separated by the specified separator.
Example:
const nameElements = [“L”,”i”,”m”,”o”,”n”];console.log(nameElements.join(‘’));//Limon
7.substr(): This method returns a string. This string stating with a specific index, ending with a specific length.
Example:const nameStr = ‘Mahbubul’;console.log(nameStr.substr(3, 6)); //bubul
7.toUppercase(): This method converts a string value to uppercase.
Example:
const subject = ‘Computer Science and Engineering’;console.log(subject.toUpperCase()); //COMPUTER SCIENCE AND ENGINEERING
8.Math.random(): This method returns a random floating-point value.
Example:
const randomNumber = Math.random();console.log(randomNumber); //generate floating-point random number between 0 to 1
9.Math.round(): This method returns a round number to the closest integer.
Example:
console.log(Math.round(4.42)); //4
10.splice (): This method created a new array by removing or replacing existing array elements.
Example:
const nums = [1,2,4,5,6];nums.splice(2, 0, 3);console.log(nums); //1, 2, 3, 4, 5, 6