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
6 comments:
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.
Seriously? This is trivial JS.
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.
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!
For most advanced JS coding, the === operator is MUCH preferred to the == operator simply because it *does* compare both value and type.
@Jay I appreciate the usefulness of '===' - I am just stating that that as an occasional js hacker I didn't know about that operator.
Post a Comment