10 Crucial JavaSctipt’s Method You Should Know!

Ridwanul Alam
3 min readMay 5, 2021

String Methods:

1. charAt():

charAt() indicates the specific character according to the given parameter, index number.

var color = "red";//console.log
color.charAt(0) = "r"
color.charAt(2) = "d"
//Note that index should be>> string.length-1

2. concat():

concat function adds or combines two or more types of string. Simply says Concat act as a jointer or glue here!

var subject = "She"
var predicate = "loves me"
concate(subject, predicate)
//result will be "She loves me"

3. includes():

It gives you a boolean result comparing two or string under a condition. An example helps you to get that concept more clearly,

const jhonySaid = "Sunny is my wife";
const jhonysRealWife = "Nusrat";
console.log(`Jhony is a ${jhonySaid.includes(jhonysRealWife)? "square shooter" : "story teller"}`)//result will be> "Jhony is a story teller"

Here includes(), justifying whether the string jhonysRealWife = “Nusrat” includes in the string jhonySaid. If includes() find “Nusrat” in the jhonySaid string then it returns True otherwise it gives False.

4. endsWith():

It Justifies whether the specific character of a string ends with under a condition and gives boolean result True or False.

const example = "Have a nice day!"
console.log(endsWith("day!")) // True
console.log(endsWith("night!")) // False

Number Methods:

5. isNaN

isNaN gives the boolean value indicating the passed value is a number or not. isNaN is true if the input is not a number & is false if it's a Number. Such as

const ten = 10
console.log(isNaN(ten)) //false
const ten = "abcd"
console.log(isNaN(ten)) //true

Math Methods:

6. Math.abs()

This function returns an absolute value of a number. It gives generally three types of output according to the input type.

The output types are given below with the example

//return 0 for,
Math.abs(null); // 0
Math.abs(''); // 0
Math.abs([]); // 0
//return absolute number for,
Math.abs([99]); //99
Math.abs(99); //99
Math.abs('-99'); //99
//return NaN for,
Math.abs([1,2]); // NaN
Math.abs({}); // NaN
Math.abs('string'); // NaN
Math.abs(); // NaN

Array Methods:

7. every():

‘every’ is an array method that tests an array whether all including elements pass the test with a given condition. It provides boolean outputs True/False.

const condition_1 = (number) => number > 10;
const condition_2 = (number) => number > 5 ;
const array = [9 , 11, 30];
console.log(array.every(condition_1)) // false
console.log(array.every(condition_2)) // true

8. forEach():

This method gives a callback function, for each element in an existing array. In general, the callback function has three arguments

  • value
  • index
  • object

Remember that forEach function always returns an undefined value.

[3,5,2].forEach(number => console.log(number+1)  
//4
//6
//3

9. map():

The map function is one of the most crucial methods for working with an array. It returns a new array. However, there are other functions that perform similar tasks,forEach is one of the alternatives to consider. But forEach method returns an undefined value on the other hand map function returns an array; It is used to pass a function in every element of an array, and finally gives a new array.

const friendsInfo = [
{ id: 1, name: 'Kaium Razu' },
{ id: 2, name: 'Jalal Mia' },
{ id: 3, name: 'Farhad Hossain' },
{ id: 4, name: 'Rafiul Alam' }
];
const friendsName = friendsInfo.map(object => object.name)
console.log(friendsName)
// ["Kaium Razu", "Jalal Mia", "Farhad Hossain", "Rafiul Alam"]

10. find()

find method is another javaScript fruitful method applied on an array and find out the definite element from the array according to the given condition. It returns an object and gives you only one value.

Suppose we consider a list of friends,

const friendsInfo = [
{ id: 1, name: 'Kaium Razu' },
{ id: 2, name: 'Jalal Mia' },
{ id: 3, name: 'Farhad Hossain' },
{ id: 4, name: 'Rafiul Alam' }
];

If we want a definite element from this array, suppose the id: 3 is the friend only we want to. Then we apply the find() method on this array;

const id3friend = friends.find(object => object.id === 3);
console.log(id3friend) // {id: 3, name: "Farhad Hossain"}

--

--

Ridwanul Alam

A Frontend Web Developer. Devoted to solving new problems and developing interactive web application