10 JavaScript Methods That Makes Coding Faster

Describing ten easy methods that makes coding faster and easier for beginners

Iffatunnessa
4 min readMay 5, 2021

1. String.prototype.concat()

The concat() method takes strings and concatenate them together as a new string.

const firstName = ‘Adam’;
const lastName = ‘Smith’;
console.log(firstName.concat(‘ ‘, lastName));
//expected output: Adam Smith

Suppose you have a form which takes inputs like first and last name individually but you want to put that together in your database or file. What you’ll do? Nothing but you have to write a method called concat().

Parameters

This method takes a string or multiple strings as parameters. Like str1, str2 and so one.

concat(str1)
concate(str1,str2)

You can take a variable or a direct string to concatenate. But before that you have take a string(str1) and call this method afterwards like this:

str1.concate(str2)

Return Value

Return Values is going to a string. If you console.log or print that, you’ll find a string as an output. As an example, we took firstName, ‘Adam’ and lastName, ‘Smith’ to concatenate and our output was ‘Adam Smith’(showed in the first example).

If you take two numbers to concatenate it’ll showed up as a string. Like:

console.log("".concat(2, 1))  // "21"

other examples:

console.log("".concat(null))  // "null"
console.log("".concat(true)) // "true"
console.log("".concat({})) // [object Object]
console.log("".concat([])) // ""

2. String.prototype.indexOf()

The indexOf() method returns a index number from a string. It can take a starting index number and return -1 if not found.

const paragraph =”I have a pet which is a cat. She loves to play with yarn ball. I also have a dog but she doesn’t want to play with me.”const pet = “cat”;console.log(paragraph.indexOf(pet));      // 24
console.log(paragraph.indexOf(pet, 30)); // -1

In this example, we are taking a paragraph where a person has a pet. We need to find if they have mentioned a cat or not. Let’s say we are searching from the first index 0 and we got 24 as a starting index of ‘cat’. But if we search from index 30, we’ll get -1 which indicated that they didn’t mention of cat. It’ll be inappropriate.

const search = "monkey";
console.log(paragraph.indexOf(search)); // -1

If we search for ‘monkey’ on the constant paragraph, we’ll get also -1 and because in that paragraph they didn’t mention ‘mokey’ at all.

3. String.prototype.trim()

The trim() method clipped off the beginning and the ending spaces, tabs, no break spaces etc. of a string.

const paragraph ="   I have a pet which is a cat.She loves to play with yarn ball.    "console.log(paragraph.trim());
//output: "I have a pet which is a cat.She loves to play with yarn ball."

But if you put a string with spaces in between the spaces it’ll not delete all the spaces and copy everything between them.

4. String.prototype.trimStart()

The trimStart() method is similar with the trim() method but it only clip off the spaces from the beginning only.

const paragraph ="   I have a pet which is a cat.She loves to play with yarn ball.    "console.log(paragraph.trim());
//output: "I have a pet which is a cat.She loves to play with yarn ball ."

5. String.prototype.trimEnd()

The trimEnd() method is similar with the trim() and trimStart() method but it only clip off the spaces from the end only.

const paragraph ="   I have a pet which is a cat.She loves to play with yarn ball.    "console.log(paragraph.trim());
//output: " I have a pet which is a cat.She loves to play with yarn ball."

6. Math.abs()

This method returns an absolute value from the number. Absolute value means a positive number. Whether, it takes a negative value, zero, null, an array or positive value.

Math.abs('-1.2'));     // 1.2
Math.abs(-1); // 1
Math.abs(null); // 0
Math.abs(''); // 0
Math.abs([]); // 0
Math.abs([2]); // 2

But if it’s an array with multiple numbers, empty object it will return NaN which means Not a Number. And if you pass a object with numbers, it’ll return a Nan also.

Math.abs([3,4]);       // NaN
Math.abs({}); // NaN
Math.abs('any string');// NaN
Math.abs(); // NaN
Math.abs({3:3}) // NaN

7. Math.floor()

This method returns a round number from input parameter. Floor means a number that is the immediate larger integer or equal to that number. Either it’s a fraction number with two numbers after decimal point or 10. It’ll return the immediate larger integer.

Math.floor(3.5) //4
Math.floor(4.00000000000000009) //5
Math.floor(3) //3

8. Math.ceil()

This method returns a round number from input parameter. Ceil is a short form of ceiling that means a number that is the immediate smaller integer or equal to that number. Either it’s a fraction number with two numbers after decimal point or 10. It’ll return the immediate small integer.

Math.ceil(3.5) //3
Math.ceil(4.00000000000000009) //4
Math.ceil(3) //3

9. Math.random()

This method randomly generates a floating point value in between the parameters. It takes two parameters with the starting and and the ending.

If we don’t put any parameters it’ll return a floating number in between 0 an 1.

console.log(Math.random());
// 0.3560930611745301
console.log(Math.random(3,4));
// 0.5033209927180251 or 0.6057698522993762 or many more

10. Number.parseInt()

The parseInt() method takes a string and converts into a number.

It takes two parameters: one is a string and another is radix which means an integer between 2 to 36. Radix is basically the base of a number.

2 : binary, 10: decimal, 8: octal, 16: hexadecimal etc..

console.log(Number.parseInt('011',2)); //3console.log(Number.parseInt('123',10)); //123console.log(Number.parseInt('011',16)); //17console.log(Number.parseInt('123',8)); //83

I hope you’ve enjoyed and learned a little from this article. Follow me for more.

--

--