10 JavaScript Methods That Makes Coding Faster

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

1. String.prototype.concat()

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

Parameters

concat(str1)
concate(str1,str2)
str1.concate(str2)
console.log("".concat(2, 1))  // "21"
console.log("".concat(null))  // "null"
console.log("".concat(true)) // "true"
console.log("".concat({})) // [object Object]
console.log("".concat([])) // ""

2. String.prototype.indexOf()

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
const search = "monkey";
console.log(paragraph.indexOf(search)); // -1

3. String.prototype.trim()

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."

4. String.prototype.trimStart()

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()

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()

Math.abs('-1.2'));     // 1.2
Math.abs(-1); // 1
Math.abs(null); // 0
Math.abs(''); // 0
Math.abs([]); // 0
Math.abs([2]); // 2
Math.abs([3,4]);       // NaN
Math.abs({}); // NaN
Math.abs('any string');// NaN
Math.abs(); // NaN
Math.abs({3:3}) // NaN

7. Math.floor()

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

8. Math.ceil()

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

9. Math.random()

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

10. Number.parseInt()

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

Software Engineer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store