JavaScript — Creating Good Habits

Devin Davis
The Startup
Published in
2 min readOct 12, 2020

Thank you for reading this post! Be sure to check out my other work on my Website, LinkedIn, and Github.

The topics I’ve touched on in this blog are based on Google’s JavaScript Style Guide. The practices I refer to correspond with Google’s preferences, are not universal, and subject to change.

The Semicolon and ASI

A semicolon explicitly terminates a statement in JavaScript. Automatic Semicolon Insertion (ASI) is the ability of JavaScript to include semicolons where necessary in code — almost perfectly. However, Google policy forbids omitting semicolons. Including semicolons can make code more readable, as well as prevent very rare and difficult to diagnose bugs resulting from the ASI.

Google’s perspective is commonly shared among seasoned engineers, many of which call the omission of semicolons bad practice. Still others say there isn’t a point in including them since the system does it for you, unless your company policy instructs otherwise.

Regardless of your personal choice on including semicolons, it is pretty universally accepted that you should at least be comfortable knowing where to include them in case you are asked to — so do not let the implementation of ASI keep you from learning how to use semicolons.

There are code editor extensions that can aid you in detecting missing semicolons, JSHint is widely recognized for this purpose.

Const or Let — Not Var

There is not a time where var should be used. When naming variables, use const or let based on your intentions for that variable.

Reminder that when a variable is set with const, it cannot be set to a new value later on. If you attempt to change its value, an error will helpfully be thrown so that the value is not overwritten. When setting a variable with let, the value can be changed without throwing an error. Const and let are both block scoped.

Single Quotes and Interpolation

Strings should be set with a single quote rather than double quotes. Use backticks where necessary — for example, when including a word with an apostrophe.

In the case of including a variable in a string, use interpolation instead of concatenation as it is more readable.

Arrow Functions and Indentation

Arrow functions are preferred, especially in cases of nested functions.

Tab is not a valid way to indent, instead — use two spaces. This is a heavily debated topic, the importance is to be consistent with your company.

Horizontal Alignment

Horizontally aligning variables by adding extra spaces is discouraged. While not strictly forbidden, the addition of white space is not considered best practice.

Thank you for taking the time to read this post. Please keep in mind that company policies vary, and Google’s preferences are not law. It is most important to follow company standards, and always be consistent in the way you write code.

Inspired by this article written by Daniel Simmons. Find Google’s JavaScript Style Guide here.

--

--

Devin Davis
The Startup

Austin, Texas — Software Engineering Student at Flatiron.