What is the difference between parseInt and + operator before a string?

I've seen a colleague converting a string to integer using the var a = +"10" f syntax but I've always used parseInt() and the line is usually var a = parseInt("10") .

Why putting the + operator before a string happens, and what is the best recommendation. Use parseInt() or operator? Do you have any influence on performance?

asked by anonymous 01.10.2018 / 14:41

3 answers


JavaScript has poor typing as well as dynamic typing. This means that it does type coercion to try to match what the programmer has done using a criterion of closest proximity, so if the code has a + operator even having only a string it thinks or a mathematical operation. I would only decide that you want a concatenation if you had two strings .

Does this seem like a wrong decision and not very intuitive? Yes, that's why people do not like JS, it's a language that tries to help so much that it gets in the way. Weak typing almost always is a mistake.

What is the solution to this? Do your best to avoid these corner case . Just because it works does not mean I should use it. Every programmer should know this. As long as someone thinks testing and running means they're right there's going to be bad code out there. When you want to convert a string to a number use the most correct function for it, and it is parseInt() . It gives clarity that this is what you want, it is not an accident.

You need to measure (in each browser, each version) to see if one gives more performance than another, but it does not matter and should not, at least nothing significant because both will need to make a relatively complex algorithm to achieve the goal, none is miraculously simple and the other very complicated, will in essence do the same thing.

I'm supportive until the language has a more rigid strict mode that can bind a limiter to this kind of thing and force it to use the most certain.

01.10.2018 / 14:51

The first syntax uses unary operator + , which converts the variable to Number.

As your question is about the difference, using the function parseInt it will also convert to Number but will truncate any decimal place that variable has for that variable to become a Number of integer type.

So if your goal is to transform a string into an integer the parseInt function is safer, but if you just want to convert the string to Number the first syntax is more succinct.


var numero = "7.5"
console.log(+numero) // 7.5
console.log(parseInt(numero)) // 7
01.10.2018 / 14:55

The type of data in an expression may confuse some script operations if the expected components of the operation are not of the right type. JavaScript tries the most to perform internal conversions to avoid such problems, but it can not read your mind. If your intentions differ from the way JavaScript handles the values, then you will not get the results you expect.

A particularly interesting case is the sum of numbers that can be in the form of text strings. In a simple arithmetic statement, which adds two numbers, you get the expected result:

var soma = 3+3;


But if one of these numbers is string, JavaScript has the tendency to convert the other value to a string - transforming the signal action more than arithmetic sum for string association (or concatenation). Therefore, in the statement 3+"3" the strength of the string in the second value prevails over the entire operation. The first value is automatically converted to string, and the result associates the two strings.

var soma = 3+"3";


See what happens when another number is added to the statement: 3 + 3 + "3"

var soma = 3 + 3 + "3";


There is a logic behind this result. The expression is evaluated from left to right. The first plus operation works on two numbers, generating the value 6. But when 6 is to be added to "3", JavaScript allows the force of string "3" to prevail. The 6 is converted to string and the two values are concatenated resulting in 63.


Note that in these cases the + operator before strings did not convert the string to integer!

If a numeric value is stored as a string, your scripts will have difficulty applying this value to a math operation. The JavaScript language provides two built-in functions for converting string representations of numbers into true numbers: parsInt () and parseFloat ().


The correct function to use is parseInt()

var soma = 3 + 3 + parseInt("3");

01.10.2018 / 16:22