Links
- ColdFusion Design Patterns
- Minecraft Papercraft Minecraft Fan? You might like my newish Minecraft Papercraft site!
- Object Oriented ColdFusion
Pages
Categories
Tags
Abstraction Ant Apache Autocomplete CFCUnit CodexWiki ColdFusion Components Constants CSS DAO Dates Dependency Injection Design Patterns File Flash Forms Frameworks Gateways Groovy Guice Hosting HTML IIS Java JavaScript jQuery Mappings MSSQL MySQL Nested Set Trees OOP pagination Python Railo Security SES URLs Spring Strategy Timestamp Tomcat Tools Ubuntu Unit Testing ValidationArchives
- February 2012
- June 2011
- February 2011
- January 2011
- December 2010
- September 2010
- July 2010
- May 2010
- April 2010
- November 2009
- July 2009
- June 2009
- August 2008
- June 2008
- May 2008
- March 2008
- November 2007
- October 2007
- September 2007
- February 2007
- January 2007
- December 2006
- November 2006
- October 2006
- September 2006
- August 2006

Another 10 Interesting JavaScript Features
I previously posted about 10 Interesting JavaScript Features. Here’s ten more JavaScript features that I’ve recently found interesting.
1. Dynamically call object methods
Javascript objects can contain functions as object members. Object members can be referenced using square bracket [] notation. Combining these ideas together we can dynamically call object methods:
2. Multi line strings
You can create multi-line strings in JavaScript by terminating lines that should be continued with a backslash:
3. Recursion in anonymous functions
JavaScript allows functions to be created that have no names. These are called anonymous functions. A recursive function is one that calls itself, but how does a function call itself if it has no name?
First let’s consider a small program that applies a factorial function to numbers in an array.
Rather that defining the factorial function separately, we can define it inline as a parameter to the applyTo() function call.
Previously, JavaScript allowed us to replace the ???WhatGoesHere??? part with arguments.callee, which was a reference to the current function.
This method still works in modern browsers but has been deprecated, so remember what it means but don’t use it.
The new method allows us to name our inline functions.
4. Short cuts with ‘or’ and ‘and’ operators
When you or together several variables in a statement, JavaScript will return first ‘truthy’ value it finds (see previous post about truthy values). This is useful when you want to use a default value if an existing variable is undefined.
In contrast, the and operator returns the last element, but only if all of the expressions are truthy, else it returns undefined.
5. A note about ‘undefined’
It turns out that undefined is not a JavaScript keyword, instead undefined is a global variable, which in browsers is defined as window.undefined.
It is not uncommon to see code that tests if a variable is undefined like this:
Which is the same as writing
If by some chance your window.undefined variable is modified then checks for undefined like this will fail. No conscientious developer would ever do this intentionally, but it may happen by accident causing hard to find bugs, like this:
A better way to test for undefined is to use the typeof operator. Typeof an undefined variable will always return the string ‘undefined’.
6. Return statement
The return statement needs it’s value to be on the same line as the return keyword. This may cause problem depending on your coding style. For example
This behaviour is due to JavaScript’s automatic semicolon insertion.
7. Length of a function
JavaScript functions have a length property that provides a count of the number of arguments the function expects.
8. The plus operator
The plus operator can be used to easily convert anything into a number by simply prefixing the value with “+”.
This is actually a just a nice shortcut for using the Number() function.
9. Object property names can be any string
JavaScript object members can have any string as their name.
Which prints:
10. The ‘debugger’ statement
In the past we were limited to using alert() statements to debug our JavaScript. After that we were rescued with the much more sane console.log(). Things have come a long way and we now have solid line debugging available in modern browsers.
However, there is one addition to our debugging arsenal which I’ve found particularly invaluable when debugging JavaScript in Internet Explorer; the debugger statement.
When you add the debugger statement to your code the browser will stop execution and open the debugging environment ready for you to continue stepping through your code.
This is moderately useful on Chrome, Firefox and Safari but I have found essential for debugging IE.
Note that within IE, this statement only works in IE7 and above and you need to first start the debug mode.
OK, so that’s 10 features, but just one more thing to wrap up which is not really a JavaScript feature.
JavaScript Coding Coventions
If you’re not following a JavaScript coding convention then here are a couple to get you started: