Tuesday, November 24, 2009

2 Obscure Javascript Features You Probably Don't Know About

I was going through the w3c javascript tutorial (I like the try it yourself sections ... yes - I am that kind of geek) and I spotted 2 pretty basic but obscure ECMAScript Javascript features I didn't know about.

Feature 1: the '===' operator!
That's right, that's a triple equal. What's it for? Well, you know javascript is not strongly typed, so you can cast a variable from a number to a string just assigning values to it. This triple = operator checks for equality of both value and type. Smart, uh? Also kinda horrible.

Feature 2: variable re-declaration!
Apparently you can re-declare a variable and the javascript interpreter couldn't care less. Moreover, if you re-declare a variable it will retain the same value as the previous homonymous variable had. Handy uh? Except it's plain twisted wrong.

Anyway, God bless ECMAScript Javascript, glorification of all sorts of butchers, and the genius dude who invented it.

6 comments:

Kinsey said...

Sorry, but this is pretty basic stuff. There is nothing horrible with the equality operator. This is for cases where you dont want "" or 0 to beinterpreted as equal to False.

And about 'redeclaring' variables, as long as the variable is already defined in the current scope (or somewhere in the scope chain) it will not be redeclared - hence it retaining the previous value.

Anonymous said...

Seriously? This is trivial JS.

Johnny Idol said...

Never claimed it's not basic stuff, I am not trying to be smart here - but I am pretty sure the average occasional javascript hacker doesn't know about that stuff (since I asked everyone I know and no one knew).

Thanks for the opinion anyway.

Christian Harms said...

Oh my god. If you dont know the difference of "==" and "===" you will have fun while coding javascript. Try to look the type conversion tutorial of Matthias on united-coders.com!

Jay said...

For most advanced JS coding, the === operator is MUCH preferred to the == operator simply because it *does* compare both value and type.

Johnny Idol said...

@Jay I appreciate the usefulness of '===' - I am just stating that that as an occasional js hacker I didn't know about that operator.