About "5" -1 and "5" +1 in Javascript (plus and minus signs)

I read a book about operators in Javascript, and it confused me.

console.log("5"+1); 

This would make "5" as a string. So the result will be 51 .

 console.log("5"-1); 

This result will be 4 . I know that it converts "5" to 5 , but why doesn't it display undefined as “string minus number”?

Update: So, what about other languages? Are they more limited?

+8
javascript add date-arithmetic
source share
8 answers

Unfortunately, it was expected from JavaScript that in the early stages of Java's success for promotions and mergers, plus for the string was accepted since Java used it.

So, JavaScript is trying to amplify strings in numbers for you, it really is, just that a plus was added for strings ... well ...

While Javascript has many advantages, it was made in 10 days and has many fun aspects like this, check out this comedy gold

+5
source share

+ is a statement that means SUM when adding numbers, which means CONCATENATE when using strings.

Like the first STRING, it will continue to concatenate "5" + toString (1).

Since the MINUS (-) operator does not work with a string, you get undefined.

If you want to use the MINUS operator, you will need:

 parseInt("5") -> It will give you 5, the number parseInt("5")-1 = 4 "5"+1 = 51 parseInt("5")+1 = 6 

Hope this helps you.

+2
source share

Because when we use "+", it can be used in two different ways:
1. as a mathematical operator.
2. combine lines

but '-' can only be used as a mathematical operator.
Therefore, javascript considers "5" numeric in the case of "-", and "5" as a string in the case of "+".

+1
source share

According to the EcmaScript 262 standard. The + and - operators behave differently when strings are involved. The first converts each value to a string. The second converts each value to a number.

From the standard:

If Type (lprim) is String or Type (rprim) is String, then return the String that is the result of combining ToString (lprim) and then ToString (rprim)

These rules imply that if an expression has a string value, all the values ​​involved in the + operation are converted to a string. In JavaScript, when the + operator is used with strings, it combines them. This is why console.log("5"+1) returns "51". 1 converted to a string, and then "5" + "1" are combined together.

However, the above rule does not apply to the - operator. When you use - , all values ​​are converted to numbers according to the standard (see below). Therefore, in this case, "5" converted to 5 , and then 1 is subtracted.

From the standard:

5 Let lnum be ToNumber (lval).

6 Let rnum be ToNumber (rval).


Operator definition from standard EcmaScript 262.

+ Operator : http://www.ecma-international.org/ecma-262/5.1/#sec-11.6.1 Operator + definition

Operator - : http://www.ecma-international.org/ecma-262/5.1/#sec-11.6.2 Operator - definition

+1
source share

In javascript (+), the operator works as described below

  • 3 + true will return 4, (+) an operator between a number and a logical or two logical values ​​will convert the boolean value to a number, so true is converted to 1, therefore, the result is 4
  • "2" + true will return "2true" if one of the operands is a string, it will convert the other operand (number or logical) to a string and process the concatenation
  • - "12" +3 will return the -9 operator, (-) before the string, convert the string to a number and make it equal to -12 and return -9
+1
source share

Due to the type of coercion and how it is not very consistent in JavaScript, in the second case, "5" converted to the number 5 , and 1 is subtracted from it, giving you 4 .

0
source share

"5" can be forcibly applied to 5 (integer). That is why you get 4 as output. However, if you try:

 console.log("text" - 1); 

The text cannot be forced, and the output is NaN

0
source share

The subtraction operator (-) subtracts the number to the right of the operator from the number to the left.

If either operand is a string, an attempt is made to convert strings to numbers. Instead of using "5" if you try console.log ("abc" - 1); it will cause an error like NaN.

For information only: The subtraction operator has special rules for handling the variety of type conversions present in JavaScript:

If the two operands are numbers, do arithmetic subtraction and return the result.

If any number is NaN, the result is NaN.

If infinity is subtracted from infinity, the result is NaN.

If -Infinity is subtracted from -Infinity, the result is NaN.

If -Infinity is subtracted from infinity, the result is infinity.

If Infinity is subtracted from -Infinity, the result is -Infinity.

If +0 is subtracted from +0, the result is +0.

If -0 is subtracted from +0, the result is -0.

If -0 is subtracted from -0, the result is +0.

If either of the two operands is not a number, the result is NaN.

0
source share

All Articles