UNPKG

34 kBMarkdownView Raw
1# JavaScript Standard Style
2
3<p align="center">
4 <a href="/docs/RULES-en.md">English</a>
5 <a href="/docs/RULES-esla.md">Español (Latinoamérica)</a>
6 <a href="/docs/RULES-fr.md">Français</a>
7 <a href="/docs/RULES-iteu.md">Italiano (Italian)</a>
8 <a href="/docs/RULES-ja.md">日本語 (Japanese)</a>
9 <a href="/docs/RULES-kokr.md">한국어 (Korean)</a>
10 <a href="/docs/RULES-ptbr.md">Português (Brasil)</a>
11 <a href="/docs/RULES-zhcn.md">简体中文 (Simplified Chinese)</a>
12 <a href="/docs/RULES-zhtw.md">繁體中文 (Taiwanese Mandarin)</a>
13</p>
14
15[![js-standard-style](https://cdn.rawgit.com/standard/standard/master/badge.svg)](https://github.com/standard/standard)
16
17This is a summary of the [standard](https://github.com/standard/standard) JavaScript
18rules.
19
20The best way to learn about `standard` is to just install it and give it a try on
21your code.
22
23## Rules
24
25* **Use 2 spaces** for indentation.
26
27 eslint: [`indent`](http://eslint.org/docs/rules/indent)
28
29 ```js
30 function hello (name) {
31 console.log('hi', name)
32 }
33 ```
34
35* **Use single quotes for strings** except to avoid escaping.
36
37 eslint: [`quotes`](http://eslint.org/docs/rules/quotes)
38
39 ```js
40 console.log('hello there') // ✓ ok
41 console.log("hello there") // ✗ avoid
42 console.log(`hello there`) // ✗ avoid
43
44 $("<div class='box'>") // ✓ ok
45 console.log(`hello ${name}`) // ✓ ok
46 ```
47
48* **No unused variables.**
49
50 eslint: [`no-unused-vars`](http://eslint.org/docs/rules/no-unused-vars)
51
52 ```js
53 function myFunction () {
54 var result = something() // ✗ avoid
55 }
56 ```
57
58* **Add a space after keywords.**
59
60 eslint: [`keyword-spacing`](http://eslint.org/docs/rules/keyword-spacing)
61
62 ```js
63 if (condition) { ... } // ✓ ok
64 if(condition) { ... } // ✗ avoid
65 ```
66
67* **Add a space before a function declaration's parentheses.**
68
69 eslint: [`space-before-function-paren`](http://eslint.org/docs/rules/space-before-function-paren)
70
71 ```js
72 function name (arg) { ... } // ✓ ok
73 function name(arg) { ... } // ✗ avoid
74
75 run(function () { ... }) // ✓ ok
76 run(function() { ... }) // ✗ avoid
77 ```
78
79* **Always use** `===` instead of `==`.<br>
80 Exception: `obj == null` is allowed to check for `null || undefined`.
81
82 eslint: [`eqeqeq`](http://eslint.org/docs/rules/eqeqeq)
83
84 ```js
85 if (name === 'John') // ✓ ok
86 if (name == 'John') // ✗ avoid
87 ```
88
89 ```js
90 if (name !== 'John') // ✓ ok
91 if (name != 'John') // ✗ avoid
92 ```
93
94* **Infix operators** must be spaced.
95
96 eslint: [`space-infix-ops`](http://eslint.org/docs/rules/space-infix-ops)
97
98 ```js
99 // ✓ ok
100 var x = 2
101 var message = 'hello, ' + name + '!'
102 ```
103
104 ```js
105 // ✗ avoid
106 var x=2
107 var message = 'hello, '+name+'!'
108 ```
109
110* **Commas should have a space** after them.
111
112 eslint: [`comma-spacing`](http://eslint.org/docs/rules/comma-spacing)
113
114 ```js
115 // ✓ ok
116 var list = [1, 2, 3, 4]
117 function greet (name, options) { ... }
118 ```
119
120 ```js
121 // ✗ avoid
122 var list = [1,2,3,4]
123 function greet (name,options) { ... }
124 ```
125
126* **Keep else statements** on the same line as their curly braces.
127
128 eslint: [`brace-style`](http://eslint.org/docs/rules/brace-style)
129
130 ```js
131 // ✓ ok
132 if (condition) {
133 // ...
134 } else {
135 // ...
136 }
137 ```
138
139 ```js
140 // ✗ avoid
141 if (condition) {
142 // ...
143 }
144 else {
145 // ...
146 }
147 ```
148
149* **For multi-line if statements,** use curly braces.
150
151 eslint: [`curly`](http://eslint.org/docs/rules/curly)
152
153 ```js
154 // ✓ ok
155 if (options.quiet !== true) console.log('done')
156 ```
157
158 ```js
159 // ✓ ok
160 if (options.quiet !== true) {
161 console.log('done')
162 }
163 ```
164
165 ```js
166 // ✗ avoid
167 if (options.quiet !== true)
168 console.log('done')
169 ```
170
171* **Always handle the** `err` function parameter.
172
173 eslint: [`handle-callback-err`](http://eslint.org/docs/rules/handle-callback-err)
174 ```js
175 // ✓ ok
176 run(function (err) {
177 if (err) throw err
178 window.alert('done')
179 })
180 ```
181
182 ```js
183 // ✗ avoid
184 run(function (err) {
185 window.alert('done')
186 })
187 ```
188
189* **Declare browser globals** with a `/* global */` comment.<br>
190 Exceptions are: `window`, `document`, and `navigator`.<br>
191 Prevents accidental use of poorly-named browser globals like `open`, `length`,
192 `event`, and `name`.
193
194 ```js
195 /* global alert, prompt */
196
197 alert('hi')
198 prompt('ok?')
199 ```
200
201 Explicitly referencing the function or property on `window` is okay too, though
202 such code will not run in a Worker which uses `self` instead of `window`.
203
204 eslint: [`no-undef`](http://eslint.org/docs/rules/no-undef)
205
206 ```js
207 window.alert('hi') // ✓ ok
208 ```
209
210* **Multiple blank lines not allowed.**
211
212 eslint: [`no-multiple-empty-lines`](http://eslint.org/docs/rules/no-multiple-empty-lines)
213
214 ```js
215 // ✓ ok
216 var value = 'hello world'
217 console.log(value)
218 ```
219
220 ```js
221 // ✗ avoid
222 var value = 'hello world'
223
224
225 console.log(value)
226 ```
227
228* **For the ternary operator** in a multi-line setting, place `?` and `:` on their own lines.
229
230 eslint: [`operator-linebreak`](http://eslint.org/docs/rules/operator-linebreak)
231
232 ```js
233 // ✓ ok
234 var location = env.development ? 'localhost' : 'www.api.com'
235
236 // ✓ ok
237 var location = env.development
238 ? 'localhost'
239 : 'www.api.com'
240
241 // ✗ avoid
242 var location = env.development ?
243 'localhost' :
244 'www.api.com'
245 ```
246
247* **For var declarations,** write each declaration in its own statement.
248
249 eslint: [`one-var`](http://eslint.org/docs/rules/one-var)
250
251 ```js
252 // ✓ ok
253 var silent = true
254 var verbose = true
255
256 // ✗ avoid
257 var silent = true, verbose = true
258
259 // ✗ avoid
260 var silent = true,
261 verbose = true
262 ```
263
264* **Wrap conditional assignments** with additional parentheses. This makes it clear that the expression is intentionally an assignment (`=`) rather than a typo for equality (`===`).
265
266 eslint: [`no-cond-assign`](http://eslint.org/docs/rules/no-cond-assign)
267
268 ```js
269 // ✓ ok
270 while ((m = text.match(expr))) {
271 // ...
272 }
273
274 // ✗ avoid
275 while (m = text.match(expr)) {
276 // ...
277 }
278 ```
279
280* **Add spaces inside single line blocks.**
281
282 eslint: [`block-spacing`](http://eslint.org/docs/rules/block-spacing)
283
284 ```js
285 function foo () {return true} // ✗ avoid
286 function foo () { return true } // ✓ ok
287 ```
288
289* **Use camelcase when naming variables and functions.**
290
291 eslint: [`camelcase`](http://eslint.org/docs/rules/camelcase)
292
293 ```js
294 function my_function () { } // ✗ avoid
295 function myFunction () { } // ✓ ok
296
297 var my_var = 'hello' // ✗ avoid
298 var myVar = 'hello' // ✓ ok
299 ```
300
301* **Trailing commas not allowed.**
302
303 eslint: [`comma-dangle`](http://eslint.org/docs/rules/comma-dangle)
304
305 ```js
306 var obj = {
307 message: 'hello', // ✗ avoid
308 }
309 ```
310
311* **Commas must be placed at the end of the current line.**
312
313 eslint: [`comma-style`](http://eslint.org/docs/rules/comma-style)
314
315 ```js
316 var obj = {
317 foo: 'foo'
318 ,bar: 'bar' // ✗ avoid
319 }
320
321 var obj = {
322 foo: 'foo',
323 bar: 'bar' // ✓ ok
324 }
325 ```
326
327* **Dot should be on the same line as property.**
328
329 eslint: [`dot-location`](http://eslint.org/docs/rules/dot-location)
330
331 ```js
332 console.
333 log('hello') // ✗ avoid
334
335 console
336 .log('hello') // ✓ ok
337 ```
338
339* **Files must end with a newline.**
340
341 eslint: [`eol-last`](http://eslint.org/docs/rules/eol-last)
342
343* **No space between function identifiers and their invocations.**
344
345 eslint: [`func-call-spacing`](http://eslint.org/docs/rules/func-call-spacing)
346
347 ```js
348 console.log ('hello') // ✗ avoid
349 console.log('hello') // ✓ ok
350 ```
351
352* **Add space between colon and value in key value pairs.**
353
354 eslint: [`key-spacing`](http://eslint.org/docs/rules/key-spacing)
355
356 ```js
357 var obj = { 'key' : 'value' } // ✗ avoid
358 var obj = { 'key' :'value' } // ✗ avoid
359 var obj = { 'key':'value' } // ✗ avoid
360 var obj = { 'key': 'value' } // ✓ ok
361 ```
362
363* **Constructor names must begin with a capital letter.**
364
365 eslint: [`new-cap`](http://eslint.org/docs/rules/new-cap)
366
367 ```js
368 function animal () {}
369 var dog = new animal() // ✗ avoid
370
371 function Animal () {}
372 var dog = new Animal() // ✓ ok
373 ```
374
375* **Constructor with no arguments must be invoked with parentheses.**
376
377 eslint: [`new-parens`](http://eslint.org/docs/rules/new-parens)
378
379 ```js
380 function Animal () {}
381 var dog = new Animal // ✗ avoid
382 var dog = new Animal() // ✓ ok
383 ```
384
385* **Objects must contain a getter when a setter is defined.**
386
387 eslint: [`accessor-pairs`](http://eslint.org/docs/rules/accessor-pairs)
388
389 ```js
390 var person = {
391 set name (value) { // ✗ avoid
392 this._name = value
393 }
394 }
395
396 var person = {
397 set name (value) {
398 this._name = value
399 },
400 get name () { // ✓ ok
401 return this._name
402 }
403 }
404 ```
405
406* **Constructors of derived classes must call `super`.**
407
408 eslint: [`constructor-super`](http://eslint.org/docs/rules/constructor-super)
409
410 ```js
411 class Dog {
412 constructor () {
413 super() // ✗ avoid
414 }
415 }
416
417 class Dog extends Mammal {
418 constructor () {
419 super() // ✓ ok
420 }
421 }
422 ```
423
424* **Use array literals instead of array constructors.**
425
426 eslint: [`no-array-constructor`](http://eslint.org/docs/rules/no-array-constructor)
427
428 ```js
429 var nums = new Array(1, 2, 3) // ✗ avoid
430 var nums = [1, 2, 3] // ✓ ok
431 ```
432
433* **Avoid using `arguments.callee` and `arguments.caller`.**
434
435 eslint: [`no-caller`](http://eslint.org/docs/rules/no-caller)
436
437 ```js
438 function foo (n) {
439 if (n <= 0) return
440
441 arguments.callee(n - 1) // ✗ avoid
442 }
443
444 function foo (n) {
445 if (n <= 0) return
446
447 foo(n - 1) // ✓ ok
448 }
449 ```
450
451* **Avoid modifying variables of class declarations.**
452
453 eslint: [`no-class-assign`](http://eslint.org/docs/rules/no-class-assign)
454
455 ```js
456 class Dog {}
457 Dog = 'Fido' // ✗ avoid
458 ```
459
460* **Avoid modifying variables declared using `const`.**
461
462 eslint: [`no-const-assign`](http://eslint.org/docs/rules/no-const-assign)
463
464 ```js
465 const score = 100
466 score = 125 // ✗ avoid
467 ```
468
469* **Avoid using constant expressions in conditions (except loops).**
470
471 eslint: [`no-constant-condition`](http://eslint.org/docs/rules/no-constant-condition)
472
473 ```js
474 if (false) { // ✗ avoid
475 // ...
476 }
477
478 if (x === 0) { // ✓ ok
479 // ...
480 }
481
482 while (true) { // ✓ ok
483 // ...
484 }
485 ```
486
487* **No control characters in regular expressions.**
488
489 eslint: [`no-control-regex`](http://eslint.org/docs/rules/no-control-regex)
490
491 ```js
492 var pattern = /\x1f/ // ✗ avoid
493 var pattern = /\x20/ // ✓ ok
494 ```
495
496* **No `debugger` statements.**
497
498 eslint: [`no-debugger`](http://eslint.org/docs/rules/no-debugger)
499
500 ```js
501 function sum (a, b) {
502 debugger // ✗ avoid
503 return a + b
504 }
505 ```
506
507* **No `delete` operator on variables.**
508
509 eslint: [`no-delete-var`](http://eslint.org/docs/rules/no-delete-var)
510
511 ```js
512 var name
513 delete name // ✗ avoid
514 ```
515
516* **No duplicate arguments in function definitions.**
517
518 eslint: [`no-dupe-args`](http://eslint.org/docs/rules/no-dupe-args)
519
520 ```js
521 function sum (a, b, a) { // ✗ avoid
522 // ...
523 }
524
525 function sum (a, b, c) { // ✓ ok
526 // ...
527 }
528 ```
529
530* **No duplicate name in class members.**
531
532 eslint: [`no-dupe-class-members`](http://eslint.org/docs/rules/no-dupe-class-members)
533
534 ```js
535 class Dog {
536 bark () {}
537 bark () {} // ✗ avoid
538 }
539 ```
540
541* **No duplicate keys in object literals.**
542
543 eslint: [`no-dupe-keys`](http://eslint.org/docs/rules/no-dupe-keys)
544
545 ```js
546 var user = {
547 name: 'Jane Doe',
548 name: 'John Doe' // ✗ avoid
549 }
550 ```
551
552* **No duplicate `case` labels in `switch` statements.**
553
554 eslint: [`no-duplicate-case`](http://eslint.org/docs/rules/no-duplicate-case)
555
556 ```js
557 switch (id) {
558 case 1:
559 // ...
560 case 1: // ✗ avoid
561 }
562 ```
563
564* **Use a single import statement per module.**
565
566 eslint: [`no-duplicate-imports`](http://eslint.org/docs/rules/no-duplicate-imports)
567
568 ```js
569 import { myFunc1 } from 'module'
570 import { myFunc2 } from 'module' // ✗ avoid
571
572 import { myFunc1, myFunc2 } from 'module' // ✓ ok
573 ```
574
575* **No empty character classes in regular expressions.**
576
577 eslint: [`no-empty-character-class`](http://eslint.org/docs/rules/no-empty-character-class)
578
579 ```js
580 const myRegex = /^abc[]/ // ✗ avoid
581 const myRegex = /^abc[a-z]/ // ✓ ok
582 ```
583
584* **No empty destructuring patterns.**
585
586 eslint: [`no-empty-pattern`](http://eslint.org/docs/rules/no-empty-pattern)
587
588 ```js
589 const { a: {} } = foo // ✗ avoid
590 const { a: { b } } = foo // ✓ ok
591 ```
592
593* **No using `eval()`.**
594
595 eslint: [`no-eval`](http://eslint.org/docs/rules/no-eval)
596
597 ```js
598 eval( "var result = user." + propName ) // ✗ avoid
599 var result = user[propName] // ✓ ok
600 ```
601
602* **No reassigning exceptions in `catch` clauses.**
603
604 eslint: [`no-ex-assign`](http://eslint.org/docs/rules/no-ex-assign)
605
606 ```js
607 try {
608 // ...
609 } catch (e) {
610 e = 'new value' // ✗ avoid
611 }
612
613 try {
614 // ...
615 } catch (e) {
616 const newVal = 'new value' // ✓ ok
617 }
618 ```
619
620* **No extending native objects.**
621
622 eslint: [`no-extend-native`](http://eslint.org/docs/rules/no-extend-native)
623
624 ```js
625 Object.prototype.age = 21 // ✗ avoid
626 ```
627
628* **Avoid unnecessary function binding.**
629
630 eslint: [`no-extra-bind`](http://eslint.org/docs/rules/no-extra-bind)
631
632 ```js
633 const name = function () {
634 getName()
635 }.bind(user) // ✗ avoid
636
637 const name = function () {
638 this.getName()
639 }.bind(user) // ✓ ok
640 ```
641
642* **Avoid unnecessary boolean casts.**
643
644 eslint: [`no-extra-boolean-cast`](http://eslint.org/docs/rules/no-extra-boolean-cast)
645
646 ```js
647 const result = true
648 if (!!result) { // ✗ avoid
649 // ...
650 }
651
652 const result = true
653 if (result) { // ✓ ok
654 // ...
655 }
656 ```
657
658* **No unnecessary parentheses around function expressions.**
659
660 eslint: [`no-extra-parens`](http://eslint.org/docs/rules/no-extra-parens)
661
662 ```js
663 const myFunc = (function () { }) // ✗ avoid
664 const myFunc = function () { } // ✓ ok
665 ```
666
667* **Use `break` to prevent fallthrough in `switch` cases.**
668
669 eslint: [`no-fallthrough`](http://eslint.org/docs/rules/no-fallthrough)
670
671 ```js
672 switch (filter) {
673 case 1:
674 doSomething() // ✗ avoid
675 case 2:
676 doSomethingElse()
677 }
678
679 switch (filter) {
680 case 1:
681 doSomething()
682 break // ✓ ok
683 case 2:
684 doSomethingElse()
685 }
686
687 switch (filter) {
688 case 1:
689 doSomething()
690 // fallthrough // ✓ ok
691 case 2:
692 doSomethingElse()
693 }
694 ```
695
696* **No floating decimals.**
697
698 eslint: [`no-floating-decimal`](http://eslint.org/docs/rules/no-floating-decimal)
699
700 ```js
701 const discount = .5 // ✗ avoid
702 const discount = 0.5 // ✓ ok
703 ```
704
705* **Avoid reassigning function declarations.**
706
707 eslint: [`no-func-assign`](http://eslint.org/docs/rules/no-func-assign)
708
709 ```js
710 function myFunc () { }
711 myFunc = myOtherFunc // ✗ avoid
712 ```
713
714* **No reassigning read-only global variables.**
715
716 eslint: [`no-global-assign`](http://eslint.org/docs/rules/no-global-assign)
717
718 ```js
719 window = {} // ✗ avoid
720 ```
721
722* **No implied `eval()`.**
723
724 eslint: [`no-implied-eval`](http://eslint.org/docs/rules/no-implied-eval)
725
726 ```js
727 setTimeout("alert('Hello world')") // ✗ avoid
728 setTimeout(function () { alert('Hello world') }) // ✓ ok
729 ```
730
731* **No function declarations in nested blocks.**
732
733 eslint: [`no-inner-declarations`](http://eslint.org/docs/rules/no-inner-declarations)
734
735 ```js
736 if (authenticated) {
737 function setAuthUser () {} // ✗ avoid
738 }
739 ```
740
741* **No invalid regular expression strings in `RegExp` constructors.**
742
743 eslint: [`no-invalid-regexp`](http://eslint.org/docs/rules/no-invalid-regexp)
744
745 ```js
746 RegExp('[a-z') // ✗ avoid
747 RegExp('[a-z]') // ✓ ok
748 ```
749
750* **No irregular whitespace.**
751
752 eslint: [`no-irregular-whitespace`](http://eslint.org/docs/rules/no-irregular-whitespace)
753
754 ```js
755 function myFunc () /*<NBSP>*/{} // ✗ avoid
756 ```
757
758* **No using `__iterator__`.**
759
760 eslint: [`no-iterator`](http://eslint.org/docs/rules/no-iterator)
761
762 ```js
763 Foo.prototype.__iterator__ = function () {} // ✗ avoid
764 ```
765
766* **No labels that share a name with an in scope variable.**
767
768 eslint: [`no-label-var`](http://eslint.org/docs/rules/no-label-var)
769
770 ```js
771 var score = 100
772 function game () {
773 score: while (true) { // ✗ avoid
774 score -= 10
775 if (score > 0) continue score
776 break
777 }
778 }
779 ```
780
781* **No label statements.**
782
783 eslint: [`no-labels`](http://eslint.org/docs/rules/no-labels)
784
785 ```js
786 label:
787 while (true) {
788 break label // ✗ avoid
789 }
790 ```
791
792* **No unnecessary nested blocks.**
793
794 eslint: [`no-lone-blocks`](http://eslint.org/docs/rules/no-lone-blocks)
795
796 ```js
797 function myFunc () {
798 { // ✗ avoid
799 myOtherFunc()
800 }
801 }
802
803 function myFunc () {
804 myOtherFunc() // ✓ ok
805 }
806 ```
807
808* **Avoid mixing spaces and tabs for indentation.**
809
810 eslint: [`no-mixed-spaces-and-tabs`](http://eslint.org/docs/rules/no-mixed-spaces-and-tabs)
811
812* **Do not use multiple spaces except for indentation.**
813
814 eslint: [`no-multi-spaces`](http://eslint.org/docs/rules/no-multi-spaces)
815
816 ```js
817 const id = 1234 // ✗ avoid
818 const id = 1234 // ✓ ok
819 ```
820
821* **No multiline strings.**
822
823 eslint: [`no-multi-str`](http://eslint.org/docs/rules/no-multi-str)
824
825 ```js
826 const message = 'Hello \
827 world' // ✗ avoid
828 ```
829
830* **No `new` without assigning object to a variable.**
831
832 eslint: [`no-new`](http://eslint.org/docs/rules/no-new)
833
834 ```js
835 new Character() // ✗ avoid
836 const character = new Character() // ✓ ok
837 ```
838
839* **No using the `Function` constructor.**
840
841 eslint: [`no-new-func`](http://eslint.org/docs/rules/no-new-func)
842
843 ```js
844 var sum = new Function('a', 'b', 'return a + b') // ✗ avoid
845 ```
846
847* **No using the `Object` constructor.**
848
849 eslint: [`no-new-object`](http://eslint.org/docs/rules/no-new-object)
850
851 ```js
852 let config = new Object() // ✗ avoid
853 ```
854
855* **No using `new require`.**
856
857 eslint: [`no-new-require`](http://eslint.org/docs/rules/no-new-require)
858
859 ```js
860 const myModule = new require('my-module') // ✗ avoid
861 ```
862
863* **No using the `Symbol` constructor.**
864
865 eslint: [`no-new-symbol`](http://eslint.org/docs/rules/no-new-symbol)
866
867 ```js
868 const foo = new Symbol('foo') // ✗ avoid
869 ```
870
871* **No using primitive wrapper instances.**
872
873 eslint: [`no-new-wrappers`](http://eslint.org/docs/rules/no-new-wrappers)
874
875 ```js
876 const message = new String('hello') // ✗ avoid
877 ```
878
879* **No calling global object properties as functions.**
880
881 eslint: [`no-obj-calls`](http://eslint.org/docs/rules/no-obj-calls)
882
883 ```js
884 const math = Math() // ✗ avoid
885 ```
886
887* **No octal literals.**
888
889 eslint: [`no-octal`](http://eslint.org/docs/rules/no-octal)
890
891 ```js
892 const octal = 042 // ✗ avoid
893 const decimal = 34 // ✓ ok
894 const octalString = '042' // ✓ ok
895 ```
896
897* **No octal escape sequences in string literals.**
898
899 eslint: [`no-octal-escape`](http://eslint.org/docs/rules/no-octal-escape)
900
901 ```js
902 const copyright = 'Copyright \251' // ✗ avoid
903 ```
904
905* **Avoid string concatenation when using `__dirname` and `__filename`.**
906
907 eslint: [`no-path-concat`](http://eslint.org/docs/rules/no-path-concat)
908
909 ```js
910 const pathToFile = __dirname + '/app.js' // ✗ avoid
911 const pathToFile = path.join(__dirname, 'app.js') // ✓ ok
912 ```
913
914* **Avoid using `__proto__`.** Use `getPrototypeOf` instead.
915
916 eslint: [`no-proto`](http://eslint.org/docs/rules/no-proto)
917
918 ```js
919 const foo = obj.__proto__ // ✗ avoid
920 const foo = Object.getPrototypeOf(obj) // ✓ ok
921 ```
922
923* **No redeclaring variables.**
924
925 eslint: [`no-redeclare`](http://eslint.org/docs/rules/no-redeclare)
926
927 ```js
928 let name = 'John'
929 let name = 'Jane' // ✗ avoid
930
931 let name = 'John'
932 name = 'Jane' // ✓ ok
933 ```
934
935* **Avoid multiple spaces in regular expression literals.**
936
937 eslint: [`no-regex-spaces`](http://eslint.org/docs/rules/no-regex-spaces)
938
939 ```js
940 const regexp = /test value/ // ✗ avoid
941
942 const regexp = /test {3}value/ // ✓ ok
943 const regexp = /test value/ // ✓ ok
944 ```
945
946* **Assignments in return statements must be surrounded by parentheses.**
947
948 eslint: [`no-return-assign`](http://eslint.org/docs/rules/no-return-assign)
949
950 ```js
951 function sum (a, b) {
952 return result = a + b // ✗ avoid
953 }
954
955 function sum (a, b) {
956 return (result = a + b) // ✓ ok
957 }
958 ```
959
960* **Avoid assigning a variable to itself**
961
962 eslint: [`no-self-assign`](http://eslint.org/docs/rules/no-self-assign)
963
964 ```js
965 name = name // ✗ avoid
966 ```
967
968* **Avoid comparing a variable to itself.**
969
970 eslint: [`no-self-compare`](http://eslint.org/docs/rules/no-self-compare)
971
972 ```js
973 if (score === score) {} // ✗ avoid
974 ```
975
976* **Avoid using the comma operator.**
977
978 eslint: [`no-sequences`](http://eslint.org/docs/rules/no-sequences)
979
980 ```js
981 if (doSomething(), !!test) {} // ✗ avoid
982 ```
983
984* **Restricted names should not be shadowed.**
985
986 eslint: [`no-shadow-restricted-names`](http://eslint.org/docs/rules/no-shadow-restricted-names)
987
988 ```js
989 let undefined = 'value' // ✗ avoid
990 ```
991
992* **Sparse arrays are not allowed.**
993
994 eslint: [`no-sparse-arrays`](http://eslint.org/docs/rules/no-sparse-arrays)
995
996 ```js
997 let fruits = ['apple',, 'orange'] // ✗ avoid
998 ```
999
1000* **Tabs should not be used**
1001
1002 eslint: [`no-tabs`](http://eslint.org/docs/rules/no-tabs)
1003
1004* **Regular strings must not contain template literal placeholders.**
1005
1006 eslint: [`no-template-curly-in-string`](http://eslint.org/docs/rules/no-template-curly-in-string)
1007
1008 ```js
1009 const message = 'Hello ${name}' // ✗ avoid
1010 const message = `Hello ${name}` // ✓ ok
1011 ```
1012
1013* **`super()` must be called before using `this`.**
1014
1015 eslint: [`no-this-before-super`](http://eslint.org/docs/rules/no-this-before-super)
1016
1017 ```js
1018 class Dog extends Animal {
1019 constructor () {
1020 this.legs = 4 // ✗ avoid
1021 super()
1022 }
1023 }
1024 ```
1025
1026* **Only `throw` an `Error` object.**
1027
1028 eslint: [`no-throw-literal`](http://eslint.org/docs/rules/no-throw-literal)
1029
1030 ```js
1031 throw 'error' // ✗ avoid
1032 throw new Error('error') // ✓ ok
1033 ```
1034
1035* **Whitespace not allowed at end of line.**
1036
1037 eslint: [`no-trailing-spaces`](http://eslint.org/docs/rules/no-trailing-spaces)
1038
1039* **Initializing to `undefined` is not allowed.**
1040
1041 eslint: [`no-undef-init`](http://eslint.org/docs/rules/no-undef-init)
1042
1043 ```js
1044 let name = undefined // ✗ avoid
1045
1046 let name
1047 name = 'value' // ✓ ok
1048 ```
1049
1050* **No unmodified conditions of loops.**
1051
1052 eslint: [`no-unmodified-loop-condition`](http://eslint.org/docs/rules/no-unmodified-loop-condition)
1053
1054 ```js
1055 for (let i = 0; i < items.length; j++) {...} // ✗ avoid
1056 for (let i = 0; i < items.length; i++) {...} // ✓ ok
1057 ```
1058
1059* **No ternary operators when simpler alternatives exist.**
1060
1061 eslint: [`no-unneeded-ternary`](http://eslint.org/docs/rules/no-unneeded-ternary)
1062
1063 ```js
1064 let score = val ? val : 0 // ✗ avoid
1065 let score = val || 0 // ✓ ok
1066 ```
1067
1068* **No unreachable code after `return`, `throw`, `continue`, and `break` statements.**
1069
1070 eslint: [`no-unreachable`](http://eslint.org/docs/rules/no-unreachable)
1071
1072 ```js
1073 function doSomething () {
1074 return true
1075 console.log('never called') // ✗ avoid
1076 }
1077 ```
1078
1079* **No flow control statements in `finally` blocks.**
1080
1081 eslint: [`no-unsafe-finally`](http://eslint.org/docs/rules/no-unsafe-finally)
1082
1083 ```js
1084 try {
1085 // ...
1086 } catch (e) {
1087 // ...
1088 } finally {
1089 return 42 // ✗ avoid
1090 }
1091 ```
1092
1093* **The left operand of relational operators must not be negated.**
1094
1095 eslint: [`no-unsafe-negation`](http://eslint.org/docs/rules/no-unsafe-negation)
1096
1097 ```js
1098 if (!key in obj) {} // ✗ avoid
1099 if (!(key in obj)) {} // ✓ ok
1100 ```
1101
1102* **Avoid unnecessary use of `.call()` and `.apply()`.**
1103
1104 eslint: [`no-useless-call`](http://eslint.org/docs/rules/no-useless-call)
1105
1106 ```js
1107 sum.call(null, 1, 2, 3) // ✗ avoid
1108 ```
1109
1110* **Avoid using unnecessary computed property keys on objects.**
1111
1112 eslint: [`no-useless-computed-key`](http://eslint.org/docs/rules/no-useless-computed-key)
1113
1114 ```js
1115 const user = { ['name']: 'John Doe' } // ✗ avoid
1116 const user = { name: 'John Doe' } // ✓ ok
1117 ```
1118
1119* **No unnecessary constructor.**
1120
1121 eslint: [`no-useless-constructor`](http://eslint.org/docs/rules/no-useless-constructor)
1122
1123 ```js
1124 class Car {
1125 constructor () { // ✗ avoid
1126 }
1127 }
1128 ```
1129
1130* **No unnecessary use of escape.**
1131
1132 eslint: [`no-useless-escape`](http://eslint.org/docs/rules/no-useless-escape)
1133
1134 ```js
1135 let message = 'Hell\o' // ✗ avoid
1136 ```
1137
1138* **Renaming import, export, and destructured assignments to the same name is not allowed.**
1139
1140 eslint: [`no-useless-rename`](http://eslint.org/docs/rules/no-useless-rename)
1141
1142 ```js
1143 import { config as config } from './config' // ✗ avoid
1144 import { config } from './config' // ✓ ok
1145 ```
1146
1147* **No whitespace before properties.**
1148
1149 eslint: [`no-whitespace-before-property`](http://eslint.org/docs/rules/no-whitespace-before-property)
1150
1151 ```js
1152 user .name // ✗ avoid
1153 user.name // ✓ ok
1154 ```
1155
1156* **No using `with` statements.**
1157
1158 eslint: [`no-with`](http://eslint.org/docs/rules/no-with)
1159
1160 ```js
1161 with (val) {...} // ✗ avoid
1162 ```
1163
1164* **Maintain consistency of newlines between object properties.**
1165
1166 eslint: [`object-property-newline`](http://eslint.org/docs/rules/object-property-newline)
1167
1168 ```js
1169 const user = {
1170 name: 'Jane Doe', age: 30,
1171 username: 'jdoe86' // ✗ avoid
1172 }
1173
1174 const user = { name: 'Jane Doe', age: 30, username: 'jdoe86' } // ✓ ok
1175
1176 const user = {
1177 name: 'Jane Doe',
1178 age: 30,
1179 username: 'jdoe86'
1180 } // ✓ ok
1181 ```
1182
1183* **No padding within blocks.**
1184
1185 eslint: [`padded-blocks`](http://eslint.org/docs/rules/padded-blocks)
1186
1187 ```js
1188 if (user) {
1189 // ✗ avoid
1190 const name = getName()
1191
1192 }
1193
1194 if (user) {
1195 const name = getName() // ✓ ok
1196 }
1197 ```
1198
1199* **No whitespace between spread operators and their expressions.**
1200
1201 eslint: [`rest-spread-spacing`](http://eslint.org/docs/rules/rest-spread-spacing)
1202
1203 ```js
1204 fn(... args) // ✗ avoid
1205 fn(...args) // ✓ ok
1206 ```
1207
1208* **Semicolons must have a space after and no space before.**
1209
1210 eslint: [`semi-spacing`](http://eslint.org/docs/rules/semi-spacing)
1211
1212 ```js
1213 for (let i = 0 ;i < items.length ;i++) {...} // ✗ avoid
1214 for (let i = 0; i < items.length; i++) {...} // ✓ ok
1215 ```
1216
1217* **Must have a space before blocks.**
1218
1219 eslint: [`space-before-blocks`](http://eslint.org/docs/rules/space-before-blocks)
1220
1221 ```js
1222 if (admin){...} // ✗ avoid
1223 if (admin) {...} // ✓ ok
1224 ```
1225
1226* **No spaces inside parentheses.**
1227
1228 eslint: [`space-in-parens`](http://eslint.org/docs/rules/space-in-parens)
1229
1230 ```js
1231 getName( name ) // ✗ avoid
1232 getName(name) // ✓ ok
1233 ```
1234
1235* **Unary operators must have a space after.**
1236
1237 eslint: [`space-unary-ops`](http://eslint.org/docs/rules/space-unary-ops)
1238
1239 ```js
1240 typeof!admin // ✗ avoid
1241 typeof !admin // ✓ ok
1242 ```
1243
1244* **Use spaces inside comments.**
1245
1246 eslint: [`spaced-comment`](http://eslint.org/docs/rules/spaced-comment)
1247
1248 ```js
1249 //comment // ✗ avoid
1250 // comment // ✓ ok
1251
1252 /*comment*/ // ✗ avoid
1253 /* comment */ // ✓ ok
1254 ```
1255
1256* **No spacing in template strings.**
1257
1258 eslint: [`template-curly-spacing`](http://eslint.org/docs/rules/template-curly-spacing)
1259
1260 ```js
1261 const message = `Hello, ${ name }` // ✗ avoid
1262 const message = `Hello, ${name}` // ✓ ok
1263 ```
1264
1265* **Use `isNaN()` when checking for `NaN`.**
1266
1267 eslint: [`use-isnan`](http://eslint.org/docs/rules/use-isnan)
1268
1269 ```js
1270 if (price === NaN) { } // ✗ avoid
1271 if (isNaN(price)) { } // ✓ ok
1272 ```
1273
1274* **`typeof` must be compared to a valid string.**
1275
1276 eslint: [`valid-typeof`](http://eslint.org/docs/rules/valid-typeof)
1277
1278 ```js
1279 typeof name === 'undefimed' // ✗ avoid
1280 typeof name === 'undefined' // ✓ ok
1281 ```
1282
1283* **Immediately Invoked Function Expressions (IIFEs) must be wrapped.**
1284
1285 eslint: [`wrap-iife`](http://eslint.org/docs/rules/wrap-iife)
1286
1287 ```js
1288 const getName = function () { }() // ✗ avoid
1289
1290 const getName = (function () { }()) // ✓ ok
1291 const getName = (function () { })() // ✓ ok
1292 ```
1293
1294* **The `*` in `yield*`expressions must have a space before and after.**
1295
1296 eslint: [`yield-star-spacing`](http://eslint.org/docs/rules/yield-star-spacing)
1297
1298 ```js
1299 yield* increment() // ✗ avoid
1300 yield * increment() // ✓ ok
1301 ```
1302
1303* **Avoid Yoda conditions.**
1304
1305 eslint: [`yoda`](http://eslint.org/docs/rules/yoda)
1306
1307 ```js
1308 if (42 === age) { } // ✗ avoid
1309 if (age === 42) { } // ✓ ok
1310 ```
1311
1312## Semicolons
1313
1314* No semicolons. (see: [1](http://blog.izs.me/post/2353458699/an-open-letter-to-javascript-leaders-regarding), [2](http://inimino.org/%7Einimino/blog/javascript_semicolons), [3](https://www.youtube.com/watch?v=gsfbh17Ax9I))
1315
1316 eslint: [`semi`](http://eslint.org/docs/rules/semi)
1317
1318 ```js
1319 window.alert('hi') // ✓ ok
1320 window.alert('hi'); // ✗ avoid
1321 ```
1322
1323* Never start a line with `(`, `[`, `` ` ``, or a handful of other unlikely possibilities.
1324
1325 This is the only gotcha with omitting semicolons, and `standard` protects you from this potential issue.
1326
1327 (The full list is: `[`, `(`, `` ` ``, `+`, `*`, `/`, `-`, `,`, `.`, but most of these will never appear at the start of a line in real code.)
1328
1329 eslint: [`no-unexpected-multiline`](http://eslint.org/docs/rules/no-unexpected-multiline)
1330
1331 ```js
1332 // ✓ ok
1333 ;(function () {
1334 window.alert('ok')
1335 }())
1336
1337 // ✗ avoid
1338 (function () {
1339 window.alert('ok')
1340 }())
1341 ```
1342
1343 ```js
1344 // ✓ ok
1345 ;[1, 2, 3].forEach(bar)
1346
1347 // ✗ avoid
1348 [1, 2, 3].forEach(bar)
1349 ```
1350
1351 ```js
1352 // ✓ ok
1353 ;`hello`.indexOf('o')
1354
1355 // ✗ avoid
1356 `hello`.indexOf('o')
1357 ```
1358
1359 Note: If you're often writing code like this, you may be trying to be too clever.
1360
1361 Clever short-hands are discouraged, in favor of clear and readable expressions, whenever
1362 possible.
1363
1364 Instead of this:
1365
1366 ```js
1367 ;[1, 2, 3].forEach(bar)
1368 ```
1369
1370 This is strongly preferred:
1371
1372 ```js
1373 var nums = [1, 2, 3]
1374 nums.forEach(bar)
1375 ```
1376
1377
1378## Helpful reading
1379
1380- [An Open Letter to JavaScript Leaders Regarding Semicolons][1]
1381- [JavaScript Semicolon Insertion – Everything you need to know][2]
1382
1383##### And a helpful video:
1384
1385- [Are Semicolons Necessary in JavaScript? - YouTube][3]
1386
1387All popular code minifiers in use today use AST-based minification, so they can
1388handle semicolon-less JavaScript with no issues (since semicolons are not required
1389in JavaScript).
1390
1391##### Excerpt from *["An Open Letter to JavaScript Leaders Regarding Semicolons"][1]*:
1392
1393> [Relying on automatic semicolon insertion] is quite safe, and perfectly valid JS that every browser understands. Closure compiler, yuicompressor, packer, and jsmin all can properly minify it. There is no performance impact anywhere.
1394>
1395> I am sorry that, instead of educating you, the leaders in this language community have given you lies and fear. That was shameful. I recommend learning how statements in JS are actually terminated (and in which cases they are not terminated), so that you can write code that you find beautiful.
1396>
1397> In general, `\n` ends a statement unless:
1398> 1. The statement has an unclosed paren, array literal, or object literal or ends in some
1399> other way that is not a valid way to end a statement. (For instance, ending with `.`
1400> or `,`.)
1401> 2. The line is `--` or `++` (in which case it will decrement/increment the next token.)
1402> 3. It is a `for()`, `while()`, `do`, `if()`, or `else`, and there is no `{`
1403> 4. The next line starts with `[`, `(`, `+`, `*`, `/`, `-`, `,`, `.`, or some other
1404> binary operator that can only be found between two tokens in a single expression.
1405>
1406> The first is pretty obvious. Even JSLint is ok with `\n` chars in JSON and parenthesized constructs, and with `var` statements that span multiple lines ending in `,`.
1407>
1408> The second is super weird. I’ve never seen a case (outside of these sorts of conversations) where you’d want to do write `i\n++\nj`, but, point of fact, that’s parsed as `i; ++j`, not `i++; j`.
1409>
1410> The third is well understood, if generally despised. `if (x)\ny()` is equivalent to `if (x) { y() }`. The construct doesn’t end until it reaches either a block, or a statement.
1411>
1412> `;` is a valid JavaScript statement, so `if(x);` is equivalent to `if(x){}` or, “If x, do nothing.” This is more commonly applied to loops where the loop check also is the update function. Unusual, but not unheard of.
1413>
1414> The fourth is generally the fud-inducing “oh noes, you need semicolons!” case. But, as it turns out, it’s quite easy to *prefix* those lines with semicolons if you don’t mean them to be continuations of the previous line. For example, instead of this:
1415>
1416> ```js
1417> foo();
1418> [1,2,3].forEach(bar);
1419> ```
1420>
1421> you could do this:
1422>
1423> ```js
1424> foo()
1425> ;[1,2,3].forEach(bar)
1426> ```
1427>
1428> The advantage is that the prefixes are easier to notice, once you are accustomed to never seeing lines starting with `(` or `[` without semis.
1429
1430[1]: http://blog.izs.me/post/2353458699/an-open-letter-to-javascript-leaders-regarding
1431[2]: http://inimino.org/~inimino/blog/javascript_semicolons
1432[3]: https://www.youtube.com/watch?v=gsfbh17Ax9I
1433
\No newline at end of file