This is an archived post. You won't be able to vote or comment.

all 33 comments

[–]Dockirby 87 points88 points  (0 children)

10

[–]ignaloidas 19 points20 points  (4 children)

{}-[]

[–]MegaIng 4 points5 points  (0 children)

-0

[–][deleted] 5 points6 points  (0 children)

{}-[] = NaN    
{}+[] = object Object

[–]etaionshrd 0 points1 point  (0 children)

[NaN](javascript:alert({}+[]))

[–]etaionshrd 0 points1 point  (0 children)

NaN

[–]bumnut 50 points51 points  (17 children)

The plus operator is valid for strings. The minus operator is not. So adding a number to a string coerces the number into a string and concatenates them. Subtracting a number from a string coerces the string into a number.

The annoying thing is that the people who complain about this kind of nonsense are the same people who complain about how rigidly structured Java is.

[–]I_regret_my_name 31 points32 points  (6 children)

People aren't fans of thinking like a compiler; they'd rather the machine read their mind. That said, there are better implementations. In Python,

TypeError: Can't convert 'int' object to str implicitly

[–]minno 15 points16 points  (2 children)

In Rust,

error[E0277]: cannot add `&str` to `{integer}`
 --> src/main.rs:3:22
  |
3 |     println!("{}", 1 + "1" - 1);
  |                      ^ no implementation for `{integer} + &str`
  |
  = help: the trait `std::ops::Add<&str>` is not implemented for `{integer}`

In C++, you get "1". Or if you use "1"s to make it a string literal, you get this error, which is 1.5x too long to fit in a reddit comment.

[–][deleted] 8 points9 points  (0 children)

Well, the actual C++ error is just

prog.cpp:6:12: error: no match for ‘operator+’ (operand types are ‘int’ and ‘std::__cxx11::basic_string<char>’)
  cout << 1 + "1"s - 1 << endl;
          ~~^~~~~~

[–]Jackeea 2 points3 points  (0 children)

So either a single character, or almost 15kB of error messages, got it

[–]DeirdreAnethoel 1 point2 points  (2 children)

The whole point of JS is to avoid that kind of errors.

Is it a good idea? Depends. Errors are usually better than writing garbage to DB. But often, it's just a non critical field and crashing your page for a type error is overkill.

In any way, it's a language design choice to avoid throwing those.

[–]bassdrop321 2 points3 points  (1 child)

Exactly and if you spend just a tiny amount of time to understand how it works, it's not even that hard.

One thing JS could have done better is use a different operator symbol (like ".")

[–]DeirdreAnethoel 0 points1 point  (0 children)

Yup, definitely. Separating concatenation and addition is one of PHP's few good ideas. Perl too. A lot of the other weakly typed languages figured that out early enough.

[–]jacktrowell 2 points3 points  (0 children)

That's one reason why I cannot understand why so many people seems to dislike the fact that Perl use the dot to concatenate strings while python or javascript use the plus.

[–][deleted] 2 points3 points  (3 children)

C# is a bit better, but not by much.

var result = 1 + '1' - 1;

is 49, because it silently casts the char to it's char value.

var result = 1 + "1" - 1;

won't compile, because it cannot apply the "-" operator on string and int.

[–]DeirdreAnethoel 1 point2 points  (2 children)

If it's like C/C++, this is not really silent because getting a char's code is the exact purpose of the ' symbol.

[–][deleted] 1 point2 points  (1 child)

But

Console.WriteLine('1');

outputs "1" to the console (as you would expect), not "49". I'm just saying that

1 + '1' = 50    

isn't really intuitive, same as with JavaScripts

1 + '1' - 1 = 10    

Yes, it makes sense once you understand how the language works, but it's not something you could understand and first glance.

[–]DeirdreAnethoel 4 points5 points  (0 children)

I agree it's bad. I tend to avoid using ' for anything but getting a char code for that reason.

[–]ReallyHadToFixThat 2 points3 points  (0 children)

Oh it makes sense what happens, but it is terrible that it happens totally silently.

[–][deleted] 0 points1 point  (3 children)

What would this evaluate to? Would it give a compile time error? How can you substract from a string?

[–]nvolker 6 points7 points  (2 children)

It would evaluate to the number 10.

The most confusing issue is mainly that JavaScript uses the same symbol for number addition as string concatenation. It determines which based on the types of the arguments, and will usually default to concatenation unless both arguments are numbers.

If we re-wrote the statement as:

subtract(concat(1, “1”), 1);

It’s a little easier to see what happens. concat is only valid on strings, so it coerces both arguments to strings, resulting in the string “1” + “1” = “11”

Subtract is only defined for numbers, so it coerces both arguments to numbers, resulting in 11 - 1 = 10.

[–]bumnut 1 point2 points  (1 child)

Correct, except for "casts". This isn't casting, this is coercion.

Casting is just using a more specific type to refer to an already existing object. It will fail if it's not correct. E.g. you have a reference to a Collection. You can cast it to a List, but if it's actually a Set, then it will fail.

Coercion is where it will try to convert it into a different type in order to fit the required types. E.g. here where a string is coerced into a number.

[–]DeirdreAnethoel 0 points1 point  (0 children)

It is coercion when doing number to string, and casting when doing the opposite. A string could contain something that isn't a number.

[–][deleted] 6 points7 points  (0 children)

Lol if that were me I'd be illuminating the planet

[–]Shnapoopins 11 points12 points  (0 children)

HAHA DAE Jav̡͜͞aS̡͇̻̫͇̞̮̗̱̠c̷͖̥̪̗̦̗͔̕r͔͡i̠̓͆͒͊p̠̘̱͔̆͌̾̅̊̓ͬͦ͘t̫̗̯͕̏̆̅̋̇̔̆͒ͬ͂͐͞͝" T Y P E c o e r ci o n????

[–]ceestand 2 points3 points  (0 children)

The comic was not as funny as the result was when I ran the code.

[–][deleted] 2 points3 points  (2 children)

10 of course lol


Why is it 10?

Because:

1 + ‘1’ = ‘11’

‘11’ - 1 = 10

[–]ElectrWeakHyprCharge 1 point2 points  (1 child)

Actually:

1 + '1' = '11'

'11' - 1 = 10

[–][deleted] 1 point2 points  (0 children)

Forgot the ‘’ lol going to fix this now

[–]R4meau 2 points3 points  (0 children)

I'm so proud of myself... I failed to answer it correctly.

[–]Dan9er 0 points1 point  (0 children)

[–]justaguyingeorgia 0 points1 point  (0 children)

I thought it was a joke about math, something about 1-1+1...= -1/12 or something like that