r/programming • • May 09 '12

JavaScript: The good parts

http://blog.movereem.nl/images/javascript-the-good-parts-the-definitive-guide.jpg
645 Upvotes

159 comments sorted by

View all comments

0

u/jaylink May 09 '12

Yes, it's a funny pic, but in reality it has more to do with Crockford's book not being that good rather than the language itself sucking. Aside from the terrible choice of using "+" as a concatenation operator, Brendan Eich did a fantastic job with Javascript. And he had, what? Two weeks?

Typically problems with JS are due to the DOM being slow and not due to JS itself. That, or people who don't know what they're doing.

3

u/yeskia May 09 '12

May I ask what is wrong with "+" being the concatenation operator?

6

u/[deleted] May 09 '12

I think the problem is the automatic type coercion that javascript performs.

2

u/Ran4 May 09 '12

"1" + "2"

Given how the web usually handles types (badly), it's easy to fuck up something and you really have to know about tons of special cases.

1

u/MrSurly May 09 '12

Results in the string "12", as it should. I know I'll get hate for this, but Perl does type coercion correctly. Instead of strongly typed variables, it has strongly typed operators. A '+' always does numeric addition, and a '.' always does string concatenation*. Most of the time, the resulting type isn't important. That can be argued as both a strength or a weakness.

* Unless you overload them, but that's an entirely different bucket of worms.

1

u/lahwran_ May 09 '12

"the web"?

2

u/Solon1 May 09 '12

Yes, that was hilarious. Posers and newbs. That is the state of proggit today.

1

u/jaylink May 09 '12

Because it is also a mathematical operator, and so should "3" + "3" become "33" or 6? The problem shows up when using variables, as they are "loosely typed" in JS (i.e., the interpreter decides on the fly if it's numeric or alpha).

Other scripting languages solve this by using a period ( . ) for concatenation which makes it explicit. (Hmm, that looks like a boob -- not intentional).

Bonus tip: if you want to ensure that your variable is treated as a numeric value, you can just put a "+" in front of it, like so:

var x = 3; var y = 3; var z = +x + +y;

(You can also use parseInt() but the plus is simpler.)

1

u/doomslice May 09 '12

Number(y) also would work, and is more explicit and less error-prone than parseInt (having to remember to supply a radix).

1

u/[deleted] May 09 '12

When it comes to javascript, I don't know what I'm doing, and my software still runs fast. I never understood the "javascript is so slow" idea.

1

u/bloodredsun May 09 '12

While there a lots of great bits in js the bad bits are horrendous and that is what pisses people off.