About "5"-1 & "5"+1 in Javascript (plus and minus signs) [duplicate]
Asked Answered
R

8

8

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?

Reconnoitre answered 27/5, 2015 at 7:23 Comment(4)
Because that's the way the JavaScript specification defines that it should be implemented.Sarawak
This question is actually a dup: see #24384288Phthalein
And "4" * "10" = 40.Craiova
@Phthalein I had no keyword at that time. :) And I think the answers below are great.Reconnoitre
T
5

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

Tribade answered 27/5, 2015 at 7:28 Comment(0)
G
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

Geophyte answered 27/5, 2015 at 7:30 Comment(2)
So using parseInt() would convert string to number. That's a little like Number().Reconnoitre
Would convert to integer. parseFloat would convert to float...Pinko
D
1

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 '+'.

Desexualize answered 27/5, 2015 at 7:29 Comment(0)
A
1

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 + definition

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

Appleby answered 27/5, 2015 at 7:38 Comment(0)
F
1

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
Farwell answered 27/5, 2015 at 7:50 Comment(0)
H
0

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.

Henkel answered 27/5, 2015 at 7:26 Comment(0)
F
0

"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

Flambeau answered 27/5, 2015 at 7:31 Comment(0)
C
0

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.

Customhouse answered 27/5, 2015 at 8:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.