About “5”-1 & “5”+1 in Javascript (plus and minus signs) [duplicate]

帅比萌擦擦* 提交于 2019-12-22 04:44:13

问题


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

console.log("5"+1);

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

console.log("5"-1);

This result would be 4. I know it converts "5" to 5, but why it isn't shown undefined as "a string minus a number"?

Update: So how about other languages? Are they more restrict?


回答1:


Sadly, it was expected from JavaScript to ride on Java's success for promotion on its early days and the plus for string concatenation was adopted since Java used it.

So JavaScript tries hard to coerce strings into numbers for you, it really does, its just that the plus was taken for strings so....well...

While Javascript has many strenghts it was made in 10 days and has many hilarious aspects like this one, check this comedy gold




回答2:


The + is a operator that means SUM when adding numbers and that means CONCATENATE when using Strings.

As the first is a STRING, it will continue concatenating a "5"+toString(1).

As the MINUS (-) operator does not work with String you are getting undefined.

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

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

Hope it will help you




回答3:


Because when we use '+' it can be used in two different ways:-
1. as mathematical operator.
2. to concatenate strings

but '-' can only be used as mathematical operator.
Hence javascript considers '5' as numerics in case of '-' while '5' as string in case of '+'.




回答4:


In javascript (+) operator operates the way described below

  • 3+true will return 4 , (+) operator between a number and a boolean or two boolean will convert boolean to number , hence true is converted to 1 hence the result is 4
  • "2"+true will return "2true" , if one of the operand is string it will convert the other operand (number or boolean) to string and process the concatenation
  • -"12"+3 will return -9 , (-) operator in front of string will convert the string to number and will make it as -12 and return -9



回答5:


According to the standard EcmaScript 262. The + and - operators behave differently when strings are involved. The first converts every value to a string. The second converts every 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 concatenating ToString(lprim) followed by ToString(rprim)

This rules implies that if in the expression there is a string value, all values involved in the + operation are converted to a string. In JavaScript when the + operator is used with strings, it concatenates them. This is why console.log("5"+1) returns "51". 1 is converted to a string and then, "5" + "1" are concatenated together.

Nevertheless, the above rule doesn't apply for the - operator. When you are using a - all values are converted to numbers according to the Standard (see below). Therefore, in this case, "5" is 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 the standard EcmaScript 262.

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

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




回答6:


Because of the type coercion and how it isn't very consistent in JavaScript, in the second case the "5" is converted to a number 5, and 1 is subtracted from it, giving you 4.




回答7:


"5" could be coerced to 5 (Integer). That's why you get 4 as output. However if you try:

console.log("text" - 1);

Text cannot be coerced, and the output is NaN




回答8:


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

When either of the operands are strings, an attempt is made to convert the strings to numbers. Instead of using "5" if you try console.log("abc" - 1); it will prompt a error as NaN.

Just for the info: The subtract operator has special rules to deal with the variety of type conversions present in JavaScript:

If the two operands are numbers, perform arithmetic subtract and return the result.

If either 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.



来源:https://stackoverflow.com/questions/30475749/about-5-1-51-in-javascript-plus-and-minus-signs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!