How to use the unary + operator In Javascript

If we use the ‘+’ operator before any string if the string in numeric it converts it to a number. 

Javascript




function convertStoI() {
    let a = "100";
    let b = +(a);
    console.log(typeof (b));
    let d = +"3 11 43";
    console.log(typeof (d));
}
convertStoI();


Output

number
number

How to convert a string into a integer without using parseInt() function in JavaScript ?

In JavaScript, there is a simple function parseInt() to convert a string to an integer. In order to know more about the function, you can refer to this. In this article, we will learn how to convert the string to an integer without using the parseInt() function.

Advantage of parseInt() over this method. The parseInt() function converts the number which is present in any base to base 10 which is not possible using the method discusses above.

Below are the following methods:

Similar Reads

Method 1: Using coercion

The very simple idea is to multiply the string by 1. If the string contains a number it will convert to an integer otherwise NaN will be returned or we can type cast to Number....

Method 2: Using the Number() function

...

Method 3: Using the unary + operator

The Number function is used to convert the parameter to the number type....

Method 4: Using Math floor() Method

...

Method 5: Using Math.ceil( ) function

If we use the ‘+’ operator before any string if the string in numeric it converts it to a number....