Airbnb JavaScript Style Guide() {
Code is first for
human…
not computer
“
Why
Style Guide?
Best practices.
More readable code
Easy maintenance
Single standard?
No, like a lot
• Airbnb
• Google
• JQuery
• W3Schools
• Node
• StandardJS
● We focus on Airbnb
References
● Use const or let for all of your
references - avoid using var
● const and let only exist in the
blocks they are defined in
● var is a function-scoped
● Use one const or let declaration
per variable
● Don't chain variable assignments
● Avoid using ++, --
Variables
● Literal syntax for object creation
● Don't use reserved words as keys
● Use object method shorthand
● Use property value shorthand
● Group your shorthand properties
at the beginning of your object
declaration.
Objects
● Use the literal syntax for array
creation
● Use Array#push instead of direct
assignment to add items to an
array
● Use spread operator ... to copy
arrays
Arrays
● Use single quotes '' for strings.
● Strings that cause the line to go
over 100 characters should not be
wrapped by concatenation
● When building up strings, use
template instead of concatenation
● Do not unnecessarily escape
characters in strings as
backslashes harm readability
Strings
Almost there...
● Use named function expressions
instead of function declarations
Function declarations are hoisted -- easy to use
before it is defined in the file -- harms
readability and maintainability
● Name the expression
● Wrap immediately invoked
function expressions in
parentheses
● Use default parameter syntax
rather than mutating function
arguments
Functions
● Put default parameters last
● Spacing in a function signature
● Never mutate parameters
● Never reassign parameters
● Multiline signatures should:
○ each item on a line by itself
○ trailing comma on the last
item
Functions
● Use === and !== over
== and !=
● Use shortcuts for booleans, but
explicit comparisons for strings
and numbers
● Ternaries should not be nested
and generally be single line
expressions
● Avoid unneeded ternary
statements
Comparison &
Equality
● Don't use iterators
● Prefer higher-order functions
map() / every() / filter() / …
and
Object.keys() / Object.values() / ...
over loops like for-in, as this
enforces immutability
Iterators
● Always comma-dangle (cleaner
diff), even in function definition
● /** ... */ multi-line comments
● // for single line comments
● // FIXME: to annotate problems
● // TODO: to annotate solutions
Commas,
Comments
Guidelines use of EcmaScript 2015
features
Modules import/export
Class & extends
String template notation
Arrow functions
Furthermore...
Linters
ESlint - Airbnb Style .eslintrc
JSHint - Airbnb Style .jshintrc
JSCS - Airbnb Style Preset
CSS
Airbnb CSS / Sass Styleguide
References
● https://github.com/airbnb/javascript

Airbnb Javascript Style Guide

  • 1.
  • 2.
    Code is firstfor human… not computer “
  • 3.
    Why Style Guide? Best practices. Morereadable code Easy maintenance
  • 4.
    Single standard? No, likea lot • Airbnb • Google • JQuery • W3Schools • Node • StandardJS ● We focus on Airbnb
  • 5.
    References ● Use constor let for all of your references - avoid using var ● const and let only exist in the blocks they are defined in ● var is a function-scoped
  • 6.
    ● Use oneconst or let declaration per variable ● Don't chain variable assignments ● Avoid using ++, -- Variables
  • 7.
    ● Literal syntaxfor object creation ● Don't use reserved words as keys ● Use object method shorthand ● Use property value shorthand ● Group your shorthand properties at the beginning of your object declaration. Objects
  • 8.
    ● Use theliteral syntax for array creation ● Use Array#push instead of direct assignment to add items to an array ● Use spread operator ... to copy arrays Arrays
  • 9.
    ● Use singlequotes '' for strings. ● Strings that cause the line to go over 100 characters should not be wrapped by concatenation ● When building up strings, use template instead of concatenation ● Do not unnecessarily escape characters in strings as backslashes harm readability Strings
  • 10.
  • 11.
    ● Use namedfunction expressions instead of function declarations Function declarations are hoisted -- easy to use before it is defined in the file -- harms readability and maintainability ● Name the expression ● Wrap immediately invoked function expressions in parentheses ● Use default parameter syntax rather than mutating function arguments Functions
  • 12.
    ● Put defaultparameters last ● Spacing in a function signature ● Never mutate parameters ● Never reassign parameters ● Multiline signatures should: ○ each item on a line by itself ○ trailing comma on the last item Functions
  • 13.
    ● Use ===and !== over == and != ● Use shortcuts for booleans, but explicit comparisons for strings and numbers ● Ternaries should not be nested and generally be single line expressions ● Avoid unneeded ternary statements Comparison & Equality
  • 14.
    ● Don't useiterators ● Prefer higher-order functions map() / every() / filter() / … and Object.keys() / Object.values() / ... over loops like for-in, as this enforces immutability Iterators
  • 15.
    ● Always comma-dangle(cleaner diff), even in function definition ● /** ... */ multi-line comments ● // for single line comments ● // FIXME: to annotate problems ● // TODO: to annotate solutions Commas, Comments
  • 16.
    Guidelines use ofEcmaScript 2015 features Modules import/export Class & extends String template notation Arrow functions Furthermore... Linters ESlint - Airbnb Style .eslintrc JSHint - Airbnb Style .jshintrc JSCS - Airbnb Style Preset CSS Airbnb CSS / Sass Styleguide
  • 17.