Review
JavaScript grammar has plenty of nuance that we as developers should spend a little more time paying closer attention to than we typically do. A little bit of effort goes a long way to solidifying your deeper knowledge of the language.
Statements and expressions have analogs in English language — statements are like sentences and expressions are like phrases. Expressions can be pure/self-contained, or they can have side effects.
The JavaScript grammar layers semantic usage rules (aka context) on top of the pure syntax. For example, { }
pairs used in various places in your program can mean statement blocks, object
literals, (ES6) destructuring assignments, or (ES6) named function arguments.
JavaScript operators all have well-defined rules for precedence (which ones bind first before others) and associativity (how multiple operator expressions are implicitly grouped). Once you learn these rules, it’s up to you to decide if precedence/associativity are too implicit for their own good, or if they will aid in writing shorter, clearer code.
ASI (Automatic Semicolon Insertion) is a parser-error-correction mechanism built into the JS engine, which allows it under certain circumstances to insert an assumed ;
in places where it is required, was omitted, and where insertion fixes the parser error. The debate rages over whether this behavior implies that most ;
are optional (and can/should be omitted for cleaner code) or whether it means that omitting them is making mistakes that the JS engine merely cleans up for you.
JavaScript has several types of errors, but it’s less known that it has two classifications for errors: “early” (compiler thrown, uncatchable) and “runtime” (try..catch
able). All syntax errors are obviously early errors that stop the program before it runs, but there are others, too.
Function arguments have an interesting relationship to their formal declared named parameters. Specifically, the arguments
array has a number of gotchas of leaky abstraction behavior if you’re not careful. Avoid arguments
if you can, but if you must use it, by all means avoid using the positional slot in arguments
at the same time as using a named parameter for that same argument.
The finally
clause attached to a try
(or try..catch
) offers some very interesting quirks in terms of execution processing order. Some of these quirks can be helpful, but it’s possible to create lots of confusion, especially if combined with labeled blocks. As always, use finally
to make code better and clearer, not more clever or confusing.
The switch
offers some nice shorthand for if..else if..
statements, but beware of many common simplifying assumptions about its behavior. There are several quirks that can trip you up if you’re not careful, but there’s also some neat hidden tricks that switch
has up its sleeve!