News for hackers. Stuff that matters.
Re:John Resig: JavaScript as a First Language by barrkel on Wednesday December 21, 2011 @02:47PM (#3378816)
It's logical and obvious if you already know the language; otherwise, it's perverse. I had to read your comment (the initial edit of it) twice to be sure you weren't being sarcastic.
Re:John Resig: JavaScript as a First Language by maximusprime on Wednesday December 21, 2011 @02:52PM (#3378844)
No it's not.

Logically, the only other thing that could happen is for an "error" or "exception" be thrown when you do

  '5' - 3
There's really only 2 choices. Either convert the '5' to a number and subtract, or throw a hissy fit because it's a string.

That's not perverse. It's very logical.

Re:John Resig: JavaScript as a First Language by rprospero on Wednesday December 21, 2011 @03:07PM (#3378909)
You're looking at the wrong side of it. The subtraction does behave in a perfectly logical manner. The problem is that, given the the behavior of the subtraction operator, the addition operator's actions are illogical. Specifically, I'd argue it's perverse because it breaks commutativity:

'5' + 3 - 3 != '5' - 3 + 3

The logical approach would be to only assume that '+' is a string concatenation if both operands are strings and otherwise type coerce into numbers. Then:

'5' + 3 = 8 '5' - 3 = 2

To someone who doesn't code Javascript for a living, the above seems like a far more consistent and useful behavior.

Re:John Resig: JavaScript as a First Language by maximusprime on Wednesday December 21, 2011 @03:12PM (#3378936)
That would be ridiculous because then what would 'hello' + 1 equal? Everyone would expect it to equal 'hello1'.
Re:John Resig: JavaScript as a First Language by rprospero on Wednesday December 21, 2011 @03:25PM (#3378986)
Except, not "everyone" would expect that.

I'd expect "hello" - 1 to return NaN, since you can't perform numerical subtraction on something that isn't a number. That's exactly what Javascript does.

In the same way, I'd expect "hello" + 1 to return Nan, since you can't perform string concatenation on something that isn't a string, and you can't perform numerical addition on something that isn't a number.

Edited for clarity

Re:John Resig: JavaScript as a First Language by maximusprime on Wednesday December 21, 2011 @03:55PM (#3379095)
"hello" - 1 doesn't return NaN out of some sense of exception to trying to subtract from a string.

It returns it because parseFloat("hello") = NaN, and NaN - 1 = NaN.

Re:John Resig: JavaScript as a First Language by barrkel on Wednesday December 21, 2011 @09:29PM (#3380077)
I would expect 'hello' + 1 to throw an exception. I would similarly expect '5' - 3 to throw an exception. Why? Because you can't add a string to an integer, and nor can you subtract an integer from a string. Doing anything else is arbitrary and unpredictable, IMO, leading to subtle type errors. You want to find type errors early as possible, rather than letting bogus values flow through the program.

I think Javascript is broken here, and Python has it right.

Re:John Resig: JavaScript as a First Language by maximusprime on Thursday December 22, 2011 @05:00AM (#3381046)
You're conveniently ignoring the fact that the + operator also means string concatenation.

Javascript thinks to itself 'hello' string concatenated with a 1 value.

Re:John Resig: JavaScript as a First Language by rimantas on Thursday December 22, 2011 @04:09AM (#3380945)
Huh? Everyone? I expect it to be 'hellp' because I am thinking it works similar to Ruby's 'hello'.succ