From the course: JavaScript: Best Practices for Code Formatting
Unlock the full course today
Join today to access over 22,400 courses taught by industry experts or purchase this course individually.
Space before and after operators - JavaScript Tutorial
From the course: JavaScript: Best Practices for Code Formatting
Space before and after operators
- [Instructor] You've probably been using operators like plus, minus and equals since your earliest math courses. And it's unlikely your teachers ever required a particular spacing regimen around them. And like most of Javascript, white space isn't significant around operators. So there's no requirement to write equations or concatenations in any specific way. However, spaces help human readability in many parts of code, and operators are no exception. To make your code more readable, you should include a space before and after every operator. Including mathematical, logical, and assignment operators. In the code for my app, I've coded up a function to calculate the distance between two sets of geo coordinates. I didn't use any spaces around operators, and the statements are pretty dense. ESLint supports flagging missing spaces with the space-infix-ops rule. An infix operator is an operator that's used between two…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
Indent consistently5m 54s
-
Space before and after operators3m 23s
-
Use a consistent format for blocks3m 58s
-
Include a space after keywords and arrows3m 2s
-
Use consistent spacing before function signature parentheses3m 37s
-
Break up and indent long-method chains2m 46s
-
Space consistently within enclosing characters7m 58s
-
Space after commas, but not before2m 14s
-
Limit blank space to one line2m 50s
-
Challenge: Optimize spacing30s
-
Solution: Optimize spacing10m 29s
-
-