Most Used Methods In JavaScript

Sourav Sarkar Emon
5 min readNov 4, 2020
Javascript Most Popular Programming Language

Almost every programmer used javascript in their daily life. It was the most popular programming language in 2019. Javascript is used by 67.8% developers.

Javascript methods are action that can be used with js all data types. There are hundreds of methods available for each data type. In all of these methods I am gonna talk about some of most used methods in Javascript.

String

Strings methods will help you to work with strings.

1. toUpperCase()

If you need to upper case the whole string. Just use this method and you will be done. The toUpperCase() method returns the string value with upper case. Here’s an example:

const str = "uppercase method";       // String
const example = str.toUpperCase(); // result: UPPERCASE METHOD

2. toLowerCase()

It is just reverse of toUpperCase() method. The toLowerCase() method returns the string value with lower case.

const str = "LOWERCASE METHOD";       // String
const example = str.toLowerCase(); // result: lowercase method

3. split()

split() method divides a string into substrings. This method convert a string into a array. We have to separate string using separator. This separator can be comma, spaces, pipe, etc.

const str = 'quick brown fox jump over the lazy dog' // String
const splits = myString.split() // return 'quick', 'brown', 'fox', 'jump', 'over', 'the', 'lazy', 'dog'.

4. trim()

the trim() method remove all whitespace from start and from end. It does not harm to the string.

const str = '     Hello world!      ';     // String
const example = str.trim(); // result: 'Hello world!';

5. includes()

The includes() method check that one word or sentences can be found on the existing string or not. This method returns the result as true or false. If the such thing can be found on the string the method returns true. Otherwise it returns false.

const str = 'quick brown fox jumps over the lazy dog';  //Stringconst exp1 = str.includes('brown');   //true
const exp2 = str.includes('red'); //false
const exp3 = str.includes('fox'); //true
const exp4 = str.includes('elephant') //false

6. indexOf()

The indexOf() method returns the index of first occurrence of specified text in a string.

const str = 'quick brown fox jumps over the lazy dog';  //Stringconst example = str.indexOf('lazy');  // result: 31;
const example = str.indexOf('b'); // result: 5;

Numbers

Numbers can be with or without decimal.

1. isNan()

The isNan() method determines wether the passed value is number or not. The result will true if returns Nan otherwise returns false.

const example = 'hello world'; 
const result1 = example.isNaN(); //true
const example2 = 420;
const result2 = example2.isNaN(); //false

2. isFinite()

The isFinite() method determines if the passed value is finite or not.

Number.isFinite(123)  //true
Number.isFinite(1/0) //false
Number.isFinite(50/5) //true
Number.isFinite(0/0) //false

3. parseFloat()

The parseFloat() method parses a passed string and returns a floating point number. If the first index is letter it will return NaN.

const exmp1 = parseFloat("420")       // return 420
const exmp2 = parseFloat("42.15) // return 42.15
const exmp3 = parseFloat("20 year") // return 20
const exmp1 = parseFloat("year 20") // NaN

4. parseInt()

The parseInt() method parses a passed string and returns a integer of specified base. If the first index is letter it will return NaN.

const exmp1 = parseInt("420")       // return 420
const exmp2 = parseInt("42.15) // return 42
const exmp3 = parseInt("20.2 year") // return 20
const exmp1 = parseInt("year 20") // NaN

Math

Math is a built in object in Javascript. Math is not a function object.

1. Math.abs()

The Math.abs() methods returns the absolute value of an number.

const exmp1 = Math.abs('-10');     // 10
const exmp2 = Math.abs(-42); // 42
const exmp3 = Math.abs(null); // 0
const exmp4 = Math.abs(''); // 0

2. Math.ceil()

The Math.ceil() methods rounds number up to the largest integar.

const exmp1 = Math.ceil(.95)         // 1
const exmp2 = Math.ceil(42.00054); // 42
const exmp3 = Math.ceil(-7.004) // -7

3. Math.floor()

The Math.floor() methods returns the nearest downwards integar. If the passed value is integar the value will be the same.

const exmp1 = Math.floor(10.95)      // 10
const exmp2 = Math.floor(42.0085); // 42
const exmp3 = Math.floor(-7.05) // -6

4. Math.min()

The Math.min() method returns the lowest value number passed into it. If there are no number then it will return NaN.

const exmp1 = Math.min(2, 3, 1);       // 1
const exmp2 = Math.min(-2, -3, -1); // -3

5. Math.max()

The Math.max() is opposite method of Math.min(). This method returns the biggest value number passed into it.

const exmp1 = Math.max(2, 3, 1);       // 3
const exmp2 = Math.max(-2, -3, -1); // -1

6. Math.sqrt()

The Math.sqrt() method returns the square root of a number that passed in.

const exmp1 = Math.sqrt(9); // 3
const exmp2 = Math.sqrt(-1); // NaN
const exmp3 = Math.sqrt(16); // 4

Array

Arrays are used to store multiple data in a single variable. Array is a global object.

1. concat()

The method concat() used to add two or more array into one. This method returns a new array.

const array1 = ['how', 'are', 'you'];
const array2 = ['I', 'am', 'fine'];
const exmp1 = array1.concat(array2); // expected output: Array ["how", "are", "you", "I", "am", "fine"]

2. every()

The every() method executes once for each element present in the array. It returns a boolean value.

const isUnder18 = (currentValue) => currentValue < 18;const old= [1, 30, 39, 29, 10, 13];
const result = array1.every(isUnder18); //return false

3. filter()

The filter() method create a new array that matched with the passed arguments requirement.

const words = ['red', 'blue', 'golden', 'yellow', 'pink'];const result = words.filter(word => word.length > 4);  // return ["golden", "yellow"]

4. find()

The find() method returns the value of the first element in an array that passed as an arguments.

const array1 = [5, 4, 18, 42, 44];const result = array1.find(element => element > 20);  // return 42

5. forEach()

The forEach() method executes a provided function once for each array element.

const array1 = ['x', 'y', 'z'];const result = array1.forEach(e => console.log(e));
// output: "x"
// output: "y"
// output: "z"

6. indexOf()

This method returns the first index of specified item after searches the array. It returns -1 if the item is not found. The indexOf method returns the position of the first occurence.

const avaengers = ["ironMan", "Hulk", "SpiderMan", "cAmerica"];const result1 = avengers.indexOf('aquaman');  // output: -1
const result2 = avengers.indexOf('Hulk'); // output: 1

7. join()

This method creates and returns a new string by concatenating all of the elements in an array. The elements will be separated by a specified separator. The default separator is comma (,).

const messages = ['I', 'Love', 'You'];const result1 = messages.join();    // output: "I,Love,You"
const result1 = messages.join(" "); // output: "I Love You"
const result1 = messages.join("-"); // output: "I-Love-You"

8. map()

The map() method creates a new array with the results of calling a function for every array element.

const numbers = [4, 9, 16, 25];const result = numbers.map(Math.sqrt);  // returns [2, 3, 4, 5]

These are some very used methods in JavaScript. These example are based on the es6 features. Who is a beginner programmer must should known all these method to be a Javascript developer.

--

--