I’d like to incorporate whatever shorthand techniques there are in my regular coding habits and also be able to read them when I see them in compacted code.
Anybody know of a reference page or documentation that outlines techniques?
Edit: I had previously mentioned minifiers and it is now clear to me that minifying and efficient JS-typing techniques are two almost-totally-separate concepts.
Updated with ECMAScript 2015 (ES6) goodies. See at the bottom.
The most common conditional shorthands are:
Object literal notation for creating Objects and Arrays:
Built in types (numbers, strings, dates, booleans)
Variable declaration:
String’s character at index:
ECMAScript 2015 (ES6) standard shorthands
These are relatively new additions so don’t expect wide support among browsers.
They may be supported by modern environments (e.g.: newer node.js) or through transpilers. The "old" versions will continue to work of course.
Arrow functions
Rest parameters
Default parameter values
Destructuring
Method definition inside object literals
Computed property names inside object literals
Bonus: new methods on built-in objects
Bonus 2: arrow functions also make
self = thiscapturing unnecessaryFinal note about types
Be careful using implicit & hidden type casting and rounding as it can lead to less readable code and some of them aren’t welcome by modern Javascript style guides.
But even those more obscure ones are helpful to understand other people’s code, reading minimized code.