Some best practice of Number and String in JavaScript

01. Determining Integer: Use Number.isInteger() function. It will return the Boolean value.

02. Use of parseInt(): We can use parseInt function both with Type parameter and without Type parameter -

const parsedValue = parseInt(String)

const parsedValue = parseInt(String, Type)

Here, if we don’t specify any type, it will treat the whole string as a Decimal Number and convert it to into a string. We can specify the type in which number system we want to treat the String. So, the types can be — 2 for binary, 8 for octal, 10 for decimal, 16 for hexadecimal. It is best practice to use the parseInt() function with its type as the second parameter.

03. The easy way of parsing to an integer: We can easily parse a string by just adding a plus sign before it. Like -

const sum = +”4" + +”5";

// The value of sum will be 9

04. Special case of parsing: If we do the plus operation of the mix of integer and string, it will specify the type of the result by the 1st element. Like -

let a = “3” + 4 + 5; // a will be “345”

let b = 3 + 4 + “5”; // b will be “75”

05. A special case of Array :

let a = [1, 2, 3, 4, 5, 6];

a[50] = 4;


Here the value of a.lenght will be 51.

06. Difference Between Primitive string and String object: This difference will be found when you want to get the eval() of a string.

let s1 = “2 + 2”; // creates a string primitive

let s2 = new String(“2 + 2”); // creates a String object

console.log(eval(s1)); // returns the number 4

console.log(eval(s2)); // returns the string “2 + 2”

07. Use of the regular expression with string:


Used to match regular expression regexp against a string.


Returns an iterator of all regexp’s matches.

08. Use of toFixed(): It is used to specifies the number of numbers after the floating-point. Like-


Here the value will be 1235.12. The parameter inside the toFixed() function specifies the number.

09. Pop, Push, Shift, unshift:


Removes the last element from an array and returns that element.


Adds one or more elements to the end of an array, and returns the new length of the array.


Removes the first element from an array and returns that element.


Adds one or more elements to the front of an array, and returns the new length of the array.

10. Array reducer:


Apply a function against an accumulator and each value of the array (from left-to-right) as to reduce it to a single value. Like -

const array1 = [1, 2, 3, 4];

const reducer = (accumulator, currentValue) => accumulator + currentValue;

// 1 + 2 + 3 + 4


// expected output: 10