I know some people cringe at the idea of writing JavaScript. They opt to write CoffeeScript instead and make every attempt to avoid looking at those funny looking mustaches.
It is however, advantageous to learn JavaScript and learn the advanced features, even the odd things about the language. Here’s a collection of things you should know:
-
Short-hand syntax for creating Arrays and Objects
Prefer to use
[]
for arrays and{}
for objects instead ofnew Array()
andnew Object()
.Other than being syntactically shorter, particularly
new Array()
yields different results depending on the arguments you provide.Passing a single
Number
argument will change the length of the array after instantiation.I’m not sure why you would do this because arrays in JavaScript are dynamic. Passing in a single argument that is not a
Number
or passing more than one argument will instantiate the array with the arguments provided.Objects are easier to build when using the short-hand syntax.
-
dot notation vs. subscript notation (bracket notation)
You can call properties/methods using dot notation or subscript notation.
You might ask why you'd ever want to use subscript notation. Subscript notation allows for dynamic creation of properties and methods. So if you ever do any sort of metaprogramming, you'll most likely be using the subscript notation.
-
=== and !===
CoffeeScript has this right. Prefer
===
and!==
over==
and!=
. The reason for this is due to type coercion when using the equality operator.JavaScript considers
0
and""
to be falsy unlike Ruby. If the strict equality operator (===
) was used in the above examples, they would all return false. -
Function Scope, The Only Scope
What is the value of
SomeObject
outside of theif
statement? Is itundefined
? Or{}
?Believe it or not, code wrapped in m̶u̶s̶t̶a̶c̶h̶e̶s̶ curly braces does not have scope. Only functions create scope. Therefore,
SomeObject
is{}
. -
Hoisting
For this example, what is the value of
SomeObject
? Is itundefined
? Or{}
? Or someError
? This is probably one of the most interesting parts of JavaScript. Right before the code is executed, all variable declarations are hoisted to the top of the given scope. The example above turns into this before execution:So, the value of
SomeObject
is{}
. Similarly, function declarations are hoisted to the top as well. This example:works because it looks like this before execution:
It's magic!
-
Casting Values
There are several ways to cast a primitive value. The simplest way is to use the
String
,Number
,Boolean
functions:Cool kids like to use this approach, which works the same way:
-
Using Number methods on Number literals
Ever wondered why you can call
String
methods onString
literals, but notNumbers
?According to the JavaScript Garden documentation, it's because a flaw in JavaScript's parser tries to parse the dot notation on a number as a floating point literal. The best way to handle this issue is to wrap the
Number
literal in parentheses. -
arguments object is not an Array
The
arguments
object has alength
property, but it is not anArray
. Therefore, you cannot directly callArray
methods from thearguments
object. The best way to do this is to usecall
orapply
. -
Privatizing variables
With closure, you can create privatized variables in JavaScript.
There are a ton more advanced features that everyone should know that are outside the scope of this post (or my lack of explaining it better than the books/articles out there):
- Prototypal inheritance
- call and apply
- function expressions vs. function declarations vs. named function expressions
- Name Resolution Order
You can read more about them below.
Master your JavaScript-Fu. Enjoy.
Reference
- JavaScript Garden
- JavaScript Patterns
- JavaScript Scoping and Hoisting
- Dmitry's JavaScript Quiz
- Kangax's JavaScript Quiz
- Named Function Expressions Demystified
- Douglas Crockford's JavaScript: The Good Parts
- Why of WAT