JavaScript Methods : In JavaScript, there are many implicit techniques. For instance, Here, the parseInt() method of the Number article is utilized to change over numeric string worth to a whole number worth. To get familiar with worked-in strategies, visit JavaScript Built-In Methods.
Perhaps you don’t care for bolt works, you would prefer not to invest an excess of energy discovering some new information, or they’re simply not important for you. Relax, you are in good company. I’ll show you how it is done in ES5 (practical deceleration) and ES6 (bolt works).
Know: Arrow capacities and capacity statements/articulations are not same and can’t be supplanted aimlessly. Remember that the this watchword works diversely between the two.
How to use Method
Item Methods in JavaScript can be gotten to by utilizing capacities. Capacities in JavaScript are put away as property estimations. The items can likewise be called without utilizing section (). In a technique, ‘this’ alludes to the proprietor object. Extra data can likewise be added alongside the article technique. Language structure: objectName.methodName()
Concat():
const a = ["Rohit Gautam", "Ajit"]; const b = ["Sandeep", "Alok", "Ankit"]; const c = ["Amit"]; const children = a.concat(b, c); console.log(children); // Output (6) ["Rohit Gautam", "Ajit", "Sandeep", "Alok", "Ankit", "Amit"]
Copywithin():
const persons = ["Rohit Gautam", "Ajit", "Alok", "Ankit"]; const copy = persons.copyWithin(2, 0); console.log(copy); //Output (4) ["Rohit Gautam", "Ajit", "Rohit Gautam", "Ajit"]
entries() :
The Array.entries() method returns an Array Iterator object with key/value pairs:
const fruits = ["Banana", "Orange", "Apple", "Mango"]; const f = fruits.entries(); console.log(f); for (let x of f) { console.log(x); } //Ouput Array Iterator {} [0, "Banana"] [1, "Orange"] [2, "Apple"] [3, "Mango"]
every():
Check if all the values in number[] are 10 or greater:
const number= [32, 33, 16, 40]; console.log(number.every(checkNumber)); function checkNumber(age) { return age > 10; } //output true
fill():
const fruits = ["Banana", "Orange", "Apple", "Mango"]; console.log(fruits.fill("Kiwi")) //output Kiwi,Kiwi,Kiwi,Kiwi
filter():
const ages = [32, 33, 16, 40]; console.log(ages.filter(checkAdult)); function checkAdult(age) { return age >= 18; } //output 32,33,40
find():
const ages = [3, 10, 18, 20]; function checkAge(age) { return age > 18; } function myFunction() { console.log(ages.find(checkAge)); } //output 20
findIndex():
const ages = [3, 10, 18, 20]; console.log(ages.findIndex(checkAge)); function checkAge(age) { return age > 18; } //output 3
forEach():
let text = ""; const fruits = ["apple", "orange", "cherry"]; fruits.forEach(myFunction); console.log(text); function myFunction(item, index) { text += index + ": " + item + "<br>"; } //output 0: apple 1: orange 2: cherry
from()
const myArr = Array.from("ABCDEFG"); console.log(myArr); //output A,B,C,D,,E,F,G
includes():
const fruits = ["Banana", "Orange", "Apple", "Mango"]; console.log(fruits.includes("Mango")); //output true
indexOf():
const fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.indexOf("Apple") // Returns 2 //output 2
isArray()
const fruits = ["Banana", "Orange", "Apple", "Mango"]; Array.isArray(fruits) // Returns true //output true
join():
const fruits = ["Banana", "Orange", "Apple", "Mango"]; console.log(fruits.join()); // output Banana,Orange,Apple,Mango
keys():
const fruits = ["Banana", "Orange", "Apple", "Mango"]; const keys = fruits.keys(); let text = ""; for (let x of keys) { text += x + "<br>"; } console.log(text ); //output 0 1 2 3
lastIndexOf()
const fruits = ["Apple", "Orange", "Apple", "Mango"]; fruits.lastIndexOf("Apple") ; //output 2
map():
const numbers = [4, 9, 16, 25]; console.log(numbers.map(Math.sqrt)); //output 2,3,4,5
pop():
const fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.pop(); // Removes "Mango" //output "Banana", "Orange", "Apple"
push()
const fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.push("Kiwi"); // Adds "Kiwi" //output Banana,Orange,Apple,Mango,Kiwi
reduce():
const numbers = [275, 50, 25]; numbers.reduce(myFunc); function myFunc(total, num) { return total - num; } //output 200
reduceRight():
const numbers = [175, 50, 25]; numbers.reduceRight(myFunc); function myFunc(total, num) { return total - num; } //output -200
reverse():
const fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.reverse(); //output Mango,Apple,Orange,Banana
shift():
const fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.shift(); //output Orange,Apple,Mango
slice():
const fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"]; const citrus = fruits.slice(1, 3); //output Orange,Lemon
some()
const ages = [3, 10, 18, 20]; ages.some(checkAdult) // Returns true function checkAdult(age) { return age >= 18; } //output true
sort():
const fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.sort(); //output Apple,Banana,Mango,Orange
splice():
const fruits = ["Banana", "Orange", "Apple", "Mango"]; // At position 2, add 2 elements: fruits.splice(2, 0, "Lemon", "Kiwi"); //ouput Banana,Orange,Lemon,Kiwi,Apple,Mango
toString():
const fruits = ["Banana", "Orange", "Apple", "Mango"]; let text = fruits.toString(); //output Banana,Orange,Apple,Mango
unShift():
const fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.unshift("Lemon","Pineapple"); //output Lemon,Pineapple,Banana,Orange,Apple,Mango
valueOf():
const fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.valueOf(); // Returns fruits //output Banana,Orange,Apple,Mango