What "Hidden Features" of JavaScript do you think every programmer should know?
After having seen the excellent quality of the answers to the following questions I thought it was time to ask it for JavaScript.
- Hidden Features of HTML
- Hidden Features of CSS
- Hidden Features of PHP
- Hidden Features of ASP.NET
- Hidden Features of C#
- Hidden Features of Java
- Hidden Features of Python
Even though JavaScript is arguably the most important Client Side language right now (just ask Google) it's surprising how little most web developers appreciate how powerful it really is.
Functions are first class citizens in JavaScript:
Functional programming techniques can be used to write elegant javascript.
Particularly, functions can be passed as parameters, e.g. Array.filter() accepts a callback:
You can also declare a "private" function that only exists within the scope of a specific function:
"Extension methods in JavaScript" via the prototype property.
This will add a
contains
method to allArray
objects. You can call this method using this syntaxI'd have to say self-executing functions.
Because Javascript doesn't have block scope, you can use a self-executing function if you want to define local variables:
Here,
myvar
is does not interfere with or pollute the global scope, and disappears when the function terminates.Prototypal inheritance (popularized by Douglas Crockford) completely revolutionizes the way you think about loads of things in Javascript.
It's a killer! Pity how almost no one uses it.
It allows you to "beget" new instances of any object, extend them, while maintaining a (live) prototypical inheritance link to their other properties. Example:
Here are some interesting things:
NaN
with anything (evenNaN
) is always false, that includes==
,<
and>
.NaN
Stands for Not a Number but if you ask for the type it actually returns a number.Array.sort
can take a comparator function and is called by a quicksort-like driver (depends on implementation).$0
,$1
,$2
members on a regex.null
is unlike anything else. It is neither an object, a boolean, a number, a string, norundefined
. It's a bit like an "alternate"undefined
. (Note:typeof null == "object"
)this
yields the otherwise unnameable [Global] object.var
, instead of just relying on automatic declaration of the variable gives the runtime a real chance of optimizing access to that variablewith
construct will destroy such optimzationsbreak
. Loops can be labeled and used as the target ofcontinue
.undefined
. (depends on implementation)if (new Boolean(false)) {...}
will execute the{...}
block[updated a little in response to good comments; please see comments]
I know I'm late to the party, but I just can't believe the
+
operator's usefulness hasn't been mentioned beyond "convert anything to a number". Maybe that's how well hidden a feature it is?Of course, you can do all this using
Number()
instead, but the+
operator is so much prettier!You can also define a numeric return value for an object by overriding the prototype's
valueOf()
method. Any number conversion performed on that object will not result inNaN
, but the return value of thevalueOf()
method: