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.
with
.It's rarely used, and frankly, rarely useful... But, in limited circumstances, it does have its uses.
For instance: object literals are quite handy for quickly setting up properties on a new object. But what if you need to change half of the properties on an existing object?
Alan Storm points out that this can be somewhat dangerous: if the object used as context doesn't have one of the properties being assigned to, it will be resolved in the outer scope, possibly creating or overwriting a global variable. This is especially dangerous if you're used to writing code to work with objects where properties with default or empty values are left undefined:
Therefore, it is probably a good idea to avoid the use of the
with
statement for such assignment.See also: Are there legitimate uses for JavaScript’s “with” statement?
You can use objects instead of switches most of the time.
Update: if you're concerned about the cases evaluating in advance being inefficient (why are you worried about efficiency this early on in the design of the program??) then you can do something like this:
This is more onerous to type (or read) than either a switch or an object, but it preserves the benefits of using an object instead of a switch, detailed in the comments section below. This style also makes it more straightforward to spin this out into a proper "class" once it grows up enough.
update2: with proposed syntax extensions for ES.next, this becomes
JavaScript does not have block scope (but it has closure so let's call it even?).
Maybe a little obvious to some...
Install Firebug and use console.log("hello"). So much better than using random alert();'s which I remember doing a lot a few years ago.
To properly remove a property from an object, you should delete the property instead of just setting it to undefined:
The property prop2 will still be part of the iteration. If you want to completely get rid of prop2, you should instead do:
The property prop2 will no longer will make an appearance when you're iterating through the properties.
Assigning default values to variables
You can use the logical or operator
||
in an assignment expression to provide a default value:The
a
variable will get the value ofc
only ifb
is falsy (if isnull
,false
,undefined
,0
,empty string
, orNaN
), otherwisea
will get the value ofb
.This is often useful in functions, when you want to give a default value to an argument in case isn't supplied:
Example IE fallback in event handlers:
The following language features have been with us for a long time, all JavaScript implementations support them, but they weren't part of the specification until ECMAScript 5th Edition:
The
debugger
statementDescribed in: § 12.15 The debugger statement
This statement allows you to put breakpoints programmatically in your code just by:
If a debugger is present or active, it will cause it to break immediately, right on that line.
Otherwise, if the debugger is not present or active this statement has no observable effect.
Multiline String literals
Described in: § 7.8.4 String Literals
You have to be careful because the character next to the
\
must be a line terminator, if you have a space after the\
for example, the code will look exactly the same, but it will raise aSyntaxError
.