ui-soxo-bootstrap-core 2.4.24

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (429) hide show
  1. package/.babelrc +9 -0
  2. package/.github/workflows/npm-publish.yml +33 -0
  3. package/.husky/pre-commit +12 -0
  4. package/.prettierrc.json +11 -0
  5. package/babel.config.js +3 -0
  6. package/core/assets/images/vector.png +0 -0
  7. package/core/components/component-loader/component-loader.js +125 -0
  8. package/core/components/component-wrapper/component-wrapper.js +122 -0
  9. package/core/components/extra-info/extra-info-details.js +172 -0
  10. package/core/components/extra-info/extra-info-details.scss +27 -0
  11. package/core/components/extra-info/extra-info.js +134 -0
  12. package/core/components/index.js +18 -0
  13. package/core/components/landing-api/landing-api.js +492 -0
  14. package/core/components/landing-api/landing-api.scss +19 -0
  15. package/core/components/menu-template-api/menu-template-api.js +321 -0
  16. package/core/components/root-application-api/root-application-api.js +174 -0
  17. package/core/components/root-application-api/root-application.api.scss +0 -0
  18. package/core/index.js +14 -0
  19. package/core/lib/Store.js +363 -0
  20. package/core/lib/assets/Tick-icon.png +0 -0
  21. package/core/lib/assets/blue-slate-theme.png +0 -0
  22. package/core/lib/assets/dark-theme.png +0 -0
  23. package/core/lib/assets/deep-purple-theme.png +0 -0
  24. package/core/lib/assets/light-theme.png +0 -0
  25. package/core/lib/assets/nura-theme.png +0 -0
  26. package/core/lib/assets/plant.png +0 -0
  27. package/core/lib/assets/welcome-image.png +0 -0
  28. package/core/lib/assets/white-haze-theme.png +0 -0
  29. package/core/lib/components/application-bootstrap/application-bootstrap.js +115 -0
  30. package/core/lib/components/application-bootstrap/application-bootstrap.scss +0 -0
  31. package/core/lib/components/approval-form/approval-form.js +280 -0
  32. package/core/lib/components/approval-form/approval-form.scss +184 -0
  33. package/core/lib/components/approval-list/approval-list.js +143 -0
  34. package/core/lib/components/approval-list/approval-list.scss +3 -0
  35. package/core/lib/components/approval-list/components/request-card/request-card.js +43 -0
  36. package/core/lib/components/approval-list/components/request-card/request-card.scss +31 -0
  37. package/core/lib/components/camera/camera.js +231 -0
  38. package/core/lib/components/camera/camera.scss +86 -0
  39. package/core/lib/components/comment-block/comment-block.js +139 -0
  40. package/core/lib/components/comment-block/comment-block.scss +3 -0
  41. package/core/lib/components/confirm-modal/confirm-modal.js +82 -0
  42. package/core/lib/components/confirm-modal/confirm-modal.scss +3 -0
  43. package/core/lib/components/consent/consent.js +67 -0
  44. package/core/lib/components/consent/consent.scss +0 -0
  45. package/core/lib/components/consent/pdf-signature.js +299 -0
  46. package/core/lib/components/consent/signature-pad.js +90 -0
  47. package/core/lib/components/consent/signature-pad.scss +14 -0
  48. package/core/lib/components/file-upload/file-upload.js +133 -0
  49. package/core/lib/components/finger-print-reader/finger-print-reader.js +296 -0
  50. package/core/lib/components/finger-print-reader/finger-print-reader.scss +47 -0
  51. package/core/lib/components/finger-print-search/finger-print-search.js +200 -0
  52. package/core/lib/components/finger-print-search/finger-print-search.scss +47 -0
  53. package/core/lib/components/global-header/animations.js +18 -0
  54. package/core/lib/components/global-header/global-header.js +413 -0
  55. package/core/lib/components/global-header/global-header.scss +369 -0
  56. package/core/lib/components/header/generic-header.js +76 -0
  57. package/core/lib/components/header/generic-header.scss +99 -0
  58. package/core/lib/components/image-preview/image-preview.js +33 -0
  59. package/core/lib/components/image-wrapper/image-wrapper.js +108 -0
  60. package/core/lib/components/image-wrapper/image-wrapper.scss +13 -0
  61. package/core/lib/components/index.js +203 -0
  62. package/core/lib/components/landing/landing.js +404 -0
  63. package/core/lib/components/landing/landing.scss +0 -0
  64. package/core/lib/components/language-switcher/language-switcher.js +50 -0
  65. package/core/lib/components/menu-context/menu-context.js +70 -0
  66. package/core/lib/components/menu-template/menu-template.js +250 -0
  67. package/core/lib/components/menu-template/menu-template.scss +9 -0
  68. package/core/lib/components/modal-search/modal-search.js +153 -0
  69. package/core/lib/components/modal-search/modal-search.scss +79 -0
  70. package/core/lib/components/modal-wrapper/modal-manager.js +16 -0
  71. package/core/lib/components/modal-wrapper/modal-wrapper.js +108 -0
  72. package/core/lib/components/modal-wrapper/modal-wrapper.scss +14 -0
  73. package/core/lib/components/notice-board/notice-board.js +132 -0
  74. package/core/lib/components/notice-board/notice-board.scss +65 -0
  75. package/core/lib/components/page-container/page-container.js +55 -0
  76. package/core/lib/components/page-container/page-container.scss +8 -0
  77. package/core/lib/components/page-header/page-header.js +23 -0
  78. package/core/lib/components/page-header/page-header.scss +18 -0
  79. package/core/lib/components/pdf-viewer/pdf-viewer.js +56 -0
  80. package/core/lib/components/portlet-table/components/table-actions/table-actions.js +58 -0
  81. package/core/lib/components/portlet-table/components/table-actions/table-actions.scss +1 -0
  82. package/core/lib/components/portlet-table/components/table-data/table-data.js +107 -0
  83. package/core/lib/components/portlet-table/components/table-data/table-data.scss +0 -0
  84. package/core/lib/components/portlet-table/portlet-table.js +63 -0
  85. package/core/lib/components/portlet-table/portlet-table.scss +90 -0
  86. package/core/lib/components/progress-bar/progress-bar.js +58 -0
  87. package/core/lib/components/progress-bar/progress-bar.scss +15 -0
  88. package/core/lib/components/request-form/request-form.js +110 -0
  89. package/core/lib/components/request-form/request-form.scss +0 -0
  90. package/core/lib/components/root-application/root-application.js +70 -0
  91. package/core/lib/components/rupee/rupee.js +14 -0
  92. package/core/lib/components/script-input/script-input.js +169 -0
  93. package/core/lib/components/script-input/script-input.scss +8 -0
  94. package/core/lib/components/sidemenu/animations.js +52 -0
  95. package/core/lib/components/sidemenu/sidemenu.js +617 -0
  96. package/core/lib/components/sidemenu/sidemenu.scss +264 -0
  97. package/core/lib/components/spotlight-search/spotlight-search.component.js +636 -0
  98. package/core/lib/components/spotlight-search/spotlight-search.component.scss +78 -0
  99. package/core/lib/components/table-wrapper/table-wrapper.js +136 -0
  100. package/core/lib/components/table-wrapper/table-wrapper.scss +72 -0
  101. package/core/lib/components/ui_elements/Loader.js +13 -0
  102. package/core/lib/components/ui_elements/Notify.js +13 -0
  103. package/core/lib/components/ui_elements/PlaceHolder.js +34 -0
  104. package/core/lib/components/web-camera/web-camera.js +162 -0
  105. package/core/lib/components/web-camera/web-camera.scss +28 -0
  106. package/core/lib/core.md +9 -0
  107. package/core/lib/elements/Elements.md +3 -0
  108. package/core/lib/elements/basic/LoggedUserRedirect.js +21 -0
  109. package/core/lib/elements/basic/PrivateRoute.js +16 -0
  110. package/core/lib/elements/basic/button/Button.md +43 -0
  111. package/core/lib/elements/basic/button/button.js +170 -0
  112. package/core/lib/elements/basic/button/button.scss +0 -0
  113. package/core/lib/elements/basic/card/Card.md +15 -0
  114. package/core/lib/elements/basic/card/card.js +40 -0
  115. package/core/lib/elements/basic/card/card.scss +14 -0
  116. package/core/lib/elements/basic/checkbox/checkbox.js +23 -0
  117. package/core/lib/elements/basic/col/col.js +16 -0
  118. package/core/lib/elements/basic/copy-to-clipboard/Readme.md +40 -0
  119. package/core/lib/elements/basic/copy-to-clipboard/copy-to-clipboard.js +62 -0
  120. package/core/lib/elements/basic/country-phone-input/Readme.md +98 -0
  121. package/core/lib/elements/basic/country-phone-input/country-phone-input.js +107 -0
  122. package/core/lib/elements/basic/country-phone-input/phone-input.scss +62 -0
  123. package/core/lib/elements/basic/datepicker/datepicker.js +33 -0
  124. package/core/lib/elements/basic/dragabble-wrapper/draggable-wrapper.js +61 -0
  125. package/core/lib/elements/basic/empty/empty.js +15 -0
  126. package/core/lib/elements/basic/fingerprint-protrected/fingerprint-protected.js +118 -0
  127. package/core/lib/elements/basic/fingerprint-protrected/fingerprint-protected.scss +10 -0
  128. package/core/lib/elements/basic/form/Readme.md +0 -0
  129. package/core/lib/elements/basic/form/form.js +70 -0
  130. package/core/lib/elements/basic/form/form.scss +4 -0
  131. package/core/lib/elements/basic/image/image.js +45 -0
  132. package/core/lib/elements/basic/image/image.scss +17 -0
  133. package/core/lib/elements/basic/image/readme.md +26 -0
  134. package/core/lib/elements/basic/image-viewer/image-viewer.js +109 -0
  135. package/core/lib/elements/basic/image-viewer/image-viewer.scss +8 -0
  136. package/core/lib/elements/basic/input/input.js +81 -0
  137. package/core/lib/elements/basic/input/readme.md +77 -0
  138. package/core/lib/elements/basic/json-input/json-input.js +51 -0
  139. package/core/lib/elements/basic/menu-dashboard/menu-dashboard.js +216 -0
  140. package/core/lib/elements/basic/menu-dashboard/menu-dashboard.scss +28 -0
  141. package/core/lib/elements/basic/modal/modal.js +64 -0
  142. package/core/lib/elements/basic/modal/readme.md +62 -0
  143. package/core/lib/elements/basic/popconfirm/popconfirm.js +18 -0
  144. package/core/lib/elements/basic/popover/popover.js +13 -0
  145. package/core/lib/elements/basic/radio/radio.js +18 -0
  146. package/core/lib/elements/basic/rangepicker/rangepicker.js +51 -0
  147. package/core/lib/elements/basic/rangepicker/rangepicker.scss +18 -0
  148. package/core/lib/elements/basic/rangepicker/readme.md +82 -0
  149. package/core/lib/elements/basic/reference-select/readme.md +19 -0
  150. package/core/lib/elements/basic/reference-select/reference-select.js +337 -0
  151. package/core/lib/elements/basic/row/row.js +16 -0
  152. package/core/lib/elements/basic/select/select.js +47 -0
  153. package/core/lib/elements/basic/select-box/readme.md +53 -0
  154. package/core/lib/elements/basic/select-box/select-box.js +63 -0
  155. package/core/lib/elements/basic/skeleton/readme.md +35 -0
  156. package/core/lib/elements/basic/skeleton/skeleton.js +36 -0
  157. package/core/lib/elements/basic/skeleton/skeleton.scss +53 -0
  158. package/core/lib/elements/basic/space/space.js +13 -0
  159. package/core/lib/elements/basic/switch/readme.md +29 -0
  160. package/core/lib/elements/basic/switch/switch.js +58 -0
  161. package/core/lib/elements/basic/tab/tab.js +15 -0
  162. package/core/lib/elements/basic/table/readme.md +9 -0
  163. package/core/lib/elements/basic/table/table.js +100 -0
  164. package/core/lib/elements/basic/table/table.scss +0 -0
  165. package/core/lib/elements/basic/tag/tag.js +63 -0
  166. package/core/lib/elements/basic/tag/tag.scss +3 -0
  167. package/core/lib/elements/basic/timeline/timeline.js +14 -0
  168. package/core/lib/elements/basic/title/readme.md +20 -0
  169. package/core/lib/elements/basic/title/title.js +38 -0
  170. package/core/lib/elements/basic/title/title.scss +0 -0
  171. package/core/lib/elements/basic/user-search/user-search.js +192 -0
  172. package/core/lib/elements/complex/barcode/barcode.js +27 -0
  173. package/core/lib/elements/complex/bargraph/bar-graph.js +262 -0
  174. package/core/lib/elements/complex/basic-table/basic-table.js +111 -0
  175. package/core/lib/elements/complex/basic-table/basic-table.scss +4 -0
  176. package/core/lib/elements/complex/date-display/date-display.js +37 -0
  177. package/core/lib/elements/complex/error-boundary/error-boundary.js +29 -0
  178. package/core/lib/elements/complex/google-location-input/map-container-library-load.js +93 -0
  179. package/core/lib/elements/complex/google-map/google-map.js +230 -0
  180. package/core/lib/elements/complex/google-map/google-map.scss +13 -0
  181. package/core/lib/elements/complex/line-graph/line-graph.js +108 -0
  182. package/core/lib/elements/complex/location-search-input/location-search-input.js +100 -0
  183. package/core/lib/elements/complex/maps/maps.js +0 -0
  184. package/core/lib/elements/complex/pie-chart/pie-chart.js +203 -0
  185. package/core/lib/elements/complex/qr-code/qr-code.js +27 -0
  186. package/core/lib/elements/complex/qrscanner/qrscanner.js +57 -0
  187. package/core/lib/elements/complex/search-debounce/search-debounce.js +37 -0
  188. package/core/lib/elements/complex/statistic-card/dashboard-statistic-card.js +76 -0
  189. package/core/lib/elements/complex/statistic-card/statistic-card.js +28 -0
  190. package/core/lib/elements/index.js +226 -0
  191. package/core/lib/hooks/device-detect.js +26 -0
  192. package/core/lib/hooks/index.js +19 -0
  193. package/core/lib/hooks/use-location.js +33 -0
  194. package/core/lib/hooks/use-window-size.js +34 -0
  195. package/core/lib/i18n.js +70 -0
  196. package/core/lib/index.js +106 -0
  197. package/core/lib/introduction.md +74 -0
  198. package/core/lib/js-styleguide.md +4112 -0
  199. package/core/lib/models/actions/actions.js +128 -0
  200. package/core/lib/models/actions/components/action-detail/action-detail.js +190 -0
  201. package/core/lib/models/actions/components/action-detail/action-detail.scss +0 -0
  202. package/core/lib/models/actions/components/custom-actions/custom-actions.js +186 -0
  203. package/core/lib/models/actions/components/custom-actions/custom-actions.scss +0 -0
  204. package/core/lib/models/attachments/attachments.js +231 -0
  205. package/core/lib/models/base-loader.js +99 -0
  206. package/core/lib/models/base.js +716 -0
  207. package/core/lib/models/branches/branches.js +125 -0
  208. package/core/lib/models/checklists/checklists.js +115 -0
  209. package/core/lib/models/columns/columns.js +169 -0
  210. package/core/lib/models/columns/components/columns-add/columns-add.js +172 -0
  211. package/core/lib/models/columns/components/columns-add/columns-add.scss +0 -0
  212. package/core/lib/models/comments/comments.js +213 -0
  213. package/core/lib/models/departments/departments.js +107 -0
  214. package/core/lib/models/financial-years/financial_years.js +127 -0
  215. package/core/lib/models/forms/components/form-creator/form-creator.js +624 -0
  216. package/core/lib/models/forms/components/form-creator/form-creator.scss +30 -0
  217. package/core/lib/models/forms/components/form-detail/form-detail.js +224 -0
  218. package/core/lib/models/forms/components/form-detail/form-detail.scss +0 -0
  219. package/core/lib/models/forms/forms.js +122 -0
  220. package/core/lib/models/index.js +203 -0
  221. package/core/lib/models/invoice-numbers/invoice_numbers.js +204 -0
  222. package/core/lib/models/lookup-types/components/lookup-detail/lookup-detail.js +145 -0
  223. package/core/lib/models/lookup-types/components/lookup-detail/lookup-detail.scss +0 -0
  224. package/core/lib/models/lookup-types/lookup-types.js +113 -0
  225. package/core/lib/models/lookup-values/components/lookup-values-add/lookup-values-add.js +126 -0
  226. package/core/lib/models/lookup-values/components/lookup-values-add/lookup-values-add.scss +0 -0
  227. package/core/lib/models/lookup-values/lookup-values.js +107 -0
  228. package/core/lib/models/menu-roles/menu-roles.js +127 -0
  229. package/core/lib/models/menus/components/menu-add/menu-add.js +228 -0
  230. package/core/lib/models/menus/components/menu-add/menu-add.scss +0 -0
  231. package/core/lib/models/menus/components/menu-detail/menu-detail.js +170 -0
  232. package/core/lib/models/menus/components/menu-detail/menu-detail.scss +0 -0
  233. package/core/lib/models/menus/components/menu-list/menu-list.js +593 -0
  234. package/core/lib/models/menus/components/menu-list/menu-list.scss +6 -0
  235. package/core/lib/models/menus/components/menu-roles-add/menu-roles-add.js +183 -0
  236. package/core/lib/models/menus/components/menu-roles-add/menu-roles-add.scss +0 -0
  237. package/core/lib/models/menus/menus.js +499 -0
  238. package/core/lib/models/models/components/model-detail/model-detail.js +137 -0
  239. package/core/lib/models/models/components/model-detail/model-detail.scss +0 -0
  240. package/core/lib/models/models/components/models.js +128 -0
  241. package/core/lib/models/modules/modules.js +204 -0
  242. package/core/lib/models/outbox/outbox.js +73 -0
  243. package/core/lib/models/pages/pages.js +107 -0
  244. package/core/lib/models/permissions/permissions.js +71 -0
  245. package/core/lib/models/process/components/process-add/process-add.js +181 -0
  246. package/core/lib/models/process/components/process-add/process-add.scss +0 -0
  247. package/core/lib/models/process/components/process-dashboard/process-dashboard.js +602 -0
  248. package/core/lib/models/process/components/process-dashboard/process-dashboard.scss +62 -0
  249. package/core/lib/models/process/components/process-detail/process-detail.js +140 -0
  250. package/core/lib/models/process/components/process-detail/process-detail.scss +0 -0
  251. package/core/lib/models/process/components/process-timeline/process-timeline.js +140 -0
  252. package/core/lib/models/process/components/task-detail/task-detail.js +240 -0
  253. package/core/lib/models/process/components/task-detail/task-detail.scss +27 -0
  254. package/core/lib/models/process/components/task-form/task-form.js +529 -0
  255. package/core/lib/models/process/components/task-form/task-form.scss +7 -0
  256. package/core/lib/models/process/components/task-list/task-list.js +221 -0
  257. package/core/lib/models/process/components/task-list/task-list.scss +14 -0
  258. package/core/lib/models/process/components/task-overview/task-overview.js +300 -0
  259. package/core/lib/models/process/components/task-overview/task-overview.scss +0 -0
  260. package/core/lib/models/process/components/task-overview-legacy/task-overview-legacy.js +192 -0
  261. package/core/lib/models/process/components/task-overview-legacy/task-overview.scss +0 -0
  262. package/core/lib/models/process/components/task-routes/task-routes.js +45 -0
  263. package/core/lib/models/process/components/task-status/task-status.js +176 -0
  264. package/core/lib/models/process/components/task-status/task-status.scss +11 -0
  265. package/core/lib/models/process/process.js +781 -0
  266. package/core/lib/models/process-transactions/process-transactions.js +124 -0
  267. package/core/lib/models/roles/roles.js +106 -0
  268. package/core/lib/models/scripts/scripts.js +111 -0
  269. package/core/lib/models/step-transactions/step-transcations.js +148 -0
  270. package/core/lib/models/steps/components/step-add/step-add.js +261 -0
  271. package/core/lib/models/steps/components/step-add/step-add.scss +0 -0
  272. package/core/lib/models/steps/components/step-detail/step-detail.js +157 -0
  273. package/core/lib/models/steps/components/step-detail/step-detail.scss +0 -0
  274. package/core/lib/models/steps/steps.js +357 -0
  275. package/core/lib/models/user-preferences/user-preferences.js +83 -0
  276. package/core/lib/models/users/components/user-add/user-add.js +226 -0
  277. package/core/lib/models/users/components/user-add/user-add.scss +0 -0
  278. package/core/lib/models/users/users.js +120 -0
  279. package/core/lib/modules/business/launch-page/launch-page.js +29 -0
  280. package/core/lib/modules/business/launch-page/launch-page.scss +6 -0
  281. package/core/lib/modules/business/slots/slots.js +231 -0
  282. package/core/lib/modules/business/slots/slots.scss +108 -0
  283. package/core/lib/modules/forms/components/field-customizer/field-customizer.js +139 -0
  284. package/core/lib/modules/forms/components/field-customizer/field-customizer.scss +0 -0
  285. package/core/lib/modules/forms/components/field-selector/field-selector.js +157 -0
  286. package/core/lib/modules/forms/components/field-selector/field-selector.scss +25 -0
  287. package/core/lib/modules/forms/components/form-display/form-display.js +203 -0
  288. package/core/lib/modules/forms/components/form-display/form-display.scss +9 -0
  289. package/core/lib/modules/forms/components/tab-customizer/tab-customizer.js +125 -0
  290. package/core/lib/modules/forms/components/tab-customizer/tab-customizer.scss +0 -0
  291. package/core/lib/modules/generic/generic-add/generic-add.js +213 -0
  292. package/core/lib/modules/generic/generic-add/generic-add.scss +0 -0
  293. package/core/lib/modules/generic/generic-detail/generic-detail.js +199 -0
  294. package/core/lib/modules/generic/generic-detail/generic-detail.scss +0 -0
  295. package/core/lib/modules/generic/generic-edit/generic-edit.js +120 -0
  296. package/core/lib/modules/generic/generic-edit/generic-edit.scss +0 -0
  297. package/core/lib/modules/generic/generic-list/ExportReactCSV.js +62 -0
  298. package/core/lib/modules/generic/generic-list/generic-list.js +705 -0
  299. package/core/lib/modules/generic/generic-list/generic-list.scss +34 -0
  300. package/core/lib/modules/generic/generic-upload/generic-upload.js +484 -0
  301. package/core/lib/modules/generic/generic-upload/generic-upload.scss +0 -0
  302. package/core/lib/modules/generic/table-settings/table-settings.js +226 -0
  303. package/core/lib/modules/generic/table-settings/table-settings.scss +37 -0
  304. package/core/lib/modules/index.js +52 -0
  305. package/core/lib/modules/modules-routes/module-routes.js +35 -0
  306. package/core/lib/modules/modules-routes/module-routes.scss +0 -0
  307. package/core/lib/pages/change-password/change-password.js +211 -0
  308. package/core/lib/pages/change-password/change-password.scss +76 -0
  309. package/core/lib/pages/homepage/homepage.js +53 -0
  310. package/core/lib/pages/index.js +20 -0
  311. package/core/lib/pages/login/login.js +617 -0
  312. package/core/lib/pages/login/login.scss +346 -0
  313. package/core/lib/pages/manage-users/manage-users.js +429 -0
  314. package/core/lib/pages/manage-users/manage-users.scss +26 -0
  315. package/core/lib/pages/profile/profile.js +247 -0
  316. package/core/lib/pages/profile/profile.scss +107 -0
  317. package/core/lib/pages/profile/theme-config.js +18 -0
  318. package/core/lib/pages/profile/themes-backup.json +311 -0
  319. package/core/lib/pages/profile/themes.json +254 -0
  320. package/core/lib/pages/register/register.js +177 -0
  321. package/core/lib/pages/register/register.scss +128 -0
  322. package/core/lib/react-styleguide.md +757 -0
  323. package/core/lib/utils/api/api.utils.js +188 -0
  324. package/core/lib/utils/api/readme.md +426 -0
  325. package/core/lib/utils/async.js +36 -0
  326. package/core/lib/utils/common/common.utils.js +123 -0
  327. package/core/lib/utils/common/readme.md +30 -0
  328. package/core/lib/utils/date/date.utils.js +295 -0
  329. package/core/lib/utils/date/readme.md +2 -0
  330. package/core/lib/utils/firebase.support.utils.js +99 -0
  331. package/core/lib/utils/firebase.utils.js +808 -0
  332. package/core/lib/utils/form/Form.md +0 -0
  333. package/core/lib/utils/form/form.utils.js +256 -0
  334. package/core/lib/utils/generic/generic.utils.js +69 -0
  335. package/core/lib/utils/http/auth.helper.js +95 -0
  336. package/core/lib/utils/http/http.utils.js +157 -0
  337. package/core/lib/utils/http/readme.md +14 -0
  338. package/core/lib/utils/index.js +46 -0
  339. package/core/lib/utils/location/location.utils.js +137 -0
  340. package/core/lib/utils/location/readme.md +18 -0
  341. package/core/lib/utils/modal.utils.js +16 -0
  342. package/core/lib/utils/notification.utils.js +35 -0
  343. package/core/lib/utils/pwa/pwa.utils.js +88 -0
  344. package/core/lib/utils/script.utils.js +235 -0
  345. package/core/lib/utils/setting.utils.js +69 -0
  346. package/core/lib/utils/upload.utils.js +30 -0
  347. package/core/models/Preference/Preferences.js +46 -0
  348. package/core/models/base/base.js +399 -0
  349. package/core/models/base-clone-loader.js +107 -0
  350. package/core/models/base-clone.js +187 -0
  351. package/core/models/base-loader.js +97 -0
  352. package/core/models/core-scripts/core-scripts.js +150 -0
  353. package/core/models/dashboard/dashboard.js +187 -0
  354. package/core/models/detail-loader.js +88 -0
  355. package/core/models/groups.js +82 -0
  356. package/core/models/index.js +94 -0
  357. package/core/models/lookup-types/components/lookup-detail/lookup-detail.js +129 -0
  358. package/core/models/lookup-types/lookup-types.js +96 -0
  359. package/core/models/lookup-values/components/lookup-values-modal/lookup-values-modal.js +95 -0
  360. package/core/models/lookup-values/components/lookup-values-modal/lookup-values-modal.scss +0 -0
  361. package/core/models/lookup-values/lookup-values.js +92 -0
  362. package/core/models/menu-roles/components/menu-roles-add/menu-roles-add.js +153 -0
  363. package/core/models/menu-roles/components/menu-roles-add/menu-roles-add.scss +0 -0
  364. package/core/models/menu-roles/menu-roles.js +158 -0
  365. package/core/models/menus/components/menu-add/menu-add.js +325 -0
  366. package/core/models/menus/components/menu-add/menu-add.scss +31 -0
  367. package/core/models/menus/components/menu-detail/menu-detail.js +263 -0
  368. package/core/models/menus/components/menu-list/menu-list.js +392 -0
  369. package/core/models/menus/components/menu-lists/menu-lists.js +429 -0
  370. package/core/models/menus/components/menu-lists/menu-lists.scss +41 -0
  371. package/core/models/menus/menus.js +291 -0
  372. package/core/models/model-columns.js +121 -0
  373. package/core/models/models/components/model-detail/model-add.js +120 -0
  374. package/core/models/models/components/model-detail/model-detail.js +133 -0
  375. package/core/models/models/components/model-detail/model-detail.scss +0 -0
  376. package/core/models/models/models.js +154 -0
  377. package/core/models/pages/components/page-add/page-add.js +163 -0
  378. package/core/models/pages/components/page-add/page-add.scss +31 -0
  379. package/core/models/pages/components/page-details/page-details.js +210 -0
  380. package/core/models/pages/components/page-list/page-list.js +248 -0
  381. package/core/models/pages/pages.js +142 -0
  382. package/core/models/pages/pages.scss +0 -0
  383. package/core/models/pages.js +142 -0
  384. package/core/models/roles/components/role-add/role-add.js +248 -0
  385. package/core/models/roles/components/role-list/role-list.js +408 -0
  386. package/core/models/roles/roles.js +188 -0
  387. package/core/models/user-roles/components/user-roles-add/user-roles-add.js +149 -0
  388. package/core/models/user-roles/components/user-roles-add/user-roles-add.scss +0 -0
  389. package/core/models/user-roles/user-roles.js +99 -0
  390. package/core/models/users/components/user-add/user-add.js +458 -0
  391. package/core/models/users/components/user-detail/user-detail.js +236 -0
  392. package/core/models/users/components/user-detail/user-detail.scss +0 -0
  393. package/core/models/users/components/user-list/user-list.js +397 -0
  394. package/core/models/users/users.js +185 -0
  395. package/core/modules/Informations/change-info/change-info.js +618 -0
  396. package/core/modules/Informations/change-info/change-info.scss +135 -0
  397. package/core/modules/dashboard/components/dashboard-card/animations.js +65 -0
  398. package/core/modules/dashboard/components/dashboard-card/dashboard-card.js +197 -0
  399. package/core/modules/dashboard/components/dashboard-card/menu-dashboard-card.js +430 -0
  400. package/core/modules/dashboard/components/dashboard-card/menu-dashboard-card.scss +60 -0
  401. package/core/modules/dashboard/components/pop-query-dashboard/pop-query-dashboard.js +66 -0
  402. package/core/modules/generic/components/generic-add/generic-add.js +121 -0
  403. package/core/modules/generic/components/generic-add/generic-add.scss +13 -0
  404. package/core/modules/generic/components/generic-add-modal/generic-add-modal.js +125 -0
  405. package/core/modules/generic/components/generic-add-modal/generic-add-modal.scss +13 -0
  406. package/core/modules/generic/components/generic-detail/generic-detail.js +184 -0
  407. package/core/modules/generic/components/generic-detail/generic-detail.scss +25 -0
  408. package/core/modules/generic/components/generic-edit/generic-edit.js +123 -0
  409. package/core/modules/generic/components/generic-edit/generic-edit.scss +0 -0
  410. package/core/modules/generic/components/generic-list/generic-list.js +335 -0
  411. package/core/modules/generic/components/generic-list/generic-list.scss +35 -0
  412. package/core/modules/index.js +54 -0
  413. package/core/modules/module-routes/module-routes.js +37 -0
  414. package/core/modules/module-routes/module-routes.scss +0 -0
  415. package/core/modules/reporting/components/reporting-dashboard/reporting-dashboard.js +1101 -0
  416. package/core/modules/reporting/components/reporting-dashboard/reporting-dashboard.scss +171 -0
  417. package/core/pages/homepage-api/homepage-api.js +106 -0
  418. package/core/pages/homepage-api/homepage-api.scss +234 -0
  419. package/core/pages/homepage-api/menu-dashboard.js +169 -0
  420. package/core/pages/homepage-api/menu-dashboard.scss +12 -0
  421. package/core/translation.json +54 -0
  422. package/core/translations.json +20 -0
  423. package/core/utils/script.utils.js +130 -0
  424. package/core/utils/settings.utils.js +26 -0
  425. package/eslint.config.mjs +79 -0
  426. package/index.js +36 -0
  427. package/package.json +118 -0
  428. package/tsconfig.json +27 -0
  429. package/webpack.config.js +174 -0
@@ -0,0 +1,4112 @@
1
+ #### Style Guide ( Adapted from Airbnb )
2
+
3
+ _A mostly reasonable approach to JavaScript_
4
+
5
+ Other Style Guides
6
+
7
+ - [ES5 (Deprecated)](https://github.com/airbnb/javascript/tree/es5-deprecated/es5)
8
+ - [React](react/)
9
+ - [CSS-in-JavaScript](https://github.com/airbnb/javascript/tree/master/css-in-javascript)
10
+ - [CSS & Sass](https://github.com/airbnb/css)
11
+
12
+ ## Table of Contents
13
+
14
+ 1. [Types](#types)
15
+ 1. [References](#references)
16
+ 1. [Objects](#objects)
17
+ 1. [Arrays](#arrays)
18
+ 1. [Destructuring](#destructuring)
19
+ 1. [Strings](#strings)
20
+ 1. [Functions](#functions)
21
+ 1. [Arrow Functions](#arrow-functions)
22
+ 1. [Classes & Constructors](#classes--constructors)
23
+ 1. [Modules](#modules)
24
+ 1. [Iterators and Generators](#iterators-and-generators)
25
+ 1. [Properties](#properties)
26
+ 1. [Variables](#variables)
27
+ 1. [Hoisting](#hoisting)
28
+ 1. [Comparison Operators & Equality](#comparison-operators--equality)
29
+ 1. [Blocks](#blocks)
30
+ 1. [Control Statements](#control-statements)
31
+ 1. [Comments](#comments)
32
+ 1. [Whitespace](#whitespace)
33
+ 1. [Commas](#commas)
34
+ 1. [Semicolons](#semicolons)
35
+ 1. [Type Casting & Coercion](#type-casting--coercion)
36
+ 1. [Naming Conventions](#naming-conventions)
37
+ 1. [Accessors](#accessors)
38
+ 1. [Events](#events)
39
+ 1. [jQuery](#jquery)
40
+ 1. [ECMAScript 5 Compatibility](#ecmascript-5-compatibility)
41
+ 1. [ECMAScript 6+ (ES 2015+) Styles](#ecmascript-6-es-2015-styles)
42
+ 1. [Standard Library](#standard-library)
43
+ 1. [Testing](#testing)
44
+ 1. [Performance](#performance)
45
+ 1. [Resources](#resources)
46
+ 1. [In the Wild](#in-the-wild)
47
+ 1. [Translation](#translation)
48
+ 1. [The JavaScript Style Guide Guide](#the-javascript-style-guide-guide)
49
+ 1. [Chat With Us About JavaScript](#chat-with-us-about-javascript)
50
+ 1. [Contributors](#contributors)
51
+ 1. [License](#license)
52
+ 1. [Amendments](#amendments)
53
+
54
+ ## Types
55
+
56
+ <a name="types--primitives"></a><a name="1.1"></a>
57
+
58
+ - [1.1](#types--primitives) **Primitives**: When you access a primitive type you work directly on its value.
59
+
60
+ - `string`
61
+ - `number`
62
+ - `boolean`
63
+ - `null`
64
+ - `undefined`
65
+ - `symbol`
66
+ - `bigint`
67
+
68
+ <br />
69
+
70
+ ```javascript static
71
+ const foo = 1;
72
+ let bar = foo;
73
+
74
+ bar = 9;
75
+
76
+ console.log(foo, bar); // => 1, 9
77
+ ```
78
+
79
+ - Symbols and BigInts cannot be faithfully polyfilled, so they should not be used when targeting browsers/environments that don’t support them natively.
80
+
81
+ <a name="types--complex"></a><a name="1.2"></a>
82
+
83
+ - [1.2](#types--complex) **Complex**: When you access a complex type you work on a reference to its value.
84
+
85
+ - `object`
86
+ - `array`
87
+ - `function`
88
+
89
+ <br />
90
+
91
+ ```javascript static
92
+ const foo = [1, 2];
93
+ const bar = foo;
94
+
95
+ bar[0] = 9;
96
+
97
+ console.log(foo[0], bar[0]); // => 9, 9
98
+ ```
99
+
100
+ **[⬆ back to top](#table-of-contents)**
101
+
102
+ ## References
103
+
104
+ <a name="references--prefer-const"></a><a name="2.1"></a>
105
+
106
+ - [2.1](#references--prefer-const) Use `const` for all of your references; avoid using `var`. eslint: [`prefer-const`](https://eslint.org/docs/rules/prefer-const), [`no-const-assign`](https://eslint.org/docs/rules/no-const-assign)
107
+
108
+ > Why? This ensures that you can’t reassign your references, which can lead to bugs and difficult to comprehend code.
109
+
110
+ ```javascript static
111
+ // bad
112
+ var a = 1;
113
+ var b = 2;
114
+
115
+ // good
116
+ const a = 1;
117
+ const b = 2;
118
+ ```
119
+
120
+ <a name="references--disallow-var"></a><a name="2.2"></a>
121
+
122
+ - [2.2](#references--disallow-var) If you must reassign references, use `let` instead of `var`. eslint: [`no-var`](https://eslint.org/docs/rules/no-var)
123
+
124
+ > Why? `let` is block-scoped rather than function-scoped like `var`.
125
+
126
+ ```javascript static
127
+ // bad
128
+ var count = 1;
129
+ if (true) {
130
+ count += 1;
131
+ }
132
+
133
+ // good, use the let.
134
+ let count = 1;
135
+ if (true) {
136
+ count += 1;
137
+ }
138
+ ```
139
+
140
+ <a name="references--block-scope"></a><a name="2.3"></a>
141
+
142
+ - [2.3](#references--block-scope) Note that both `let` and `const` are block-scoped, whereas `var` is function-scoped.
143
+
144
+ ```javascript static
145
+ // const and let only exist in the blocks they are defined in.
146
+ {
147
+ let a = 1;
148
+ const b = 1;
149
+ var c = 1;
150
+ }
151
+ console.log(a); // ReferenceError
152
+ console.log(b); // ReferenceError
153
+ console.log(c); // Prints 1
154
+ ```
155
+
156
+ In the above code, you can see that referencing `a` and `b` will produce a ReferenceError, while `c` contains the number. This is because `a` and `b` are block scoped, while `c` is scoped to the containing function.
157
+
158
+ **[⬆ back to top](#table-of-contents)**
159
+
160
+ ## Objects
161
+
162
+ <a name="objects--no-new"></a><a name="3.1"></a>
163
+
164
+ - [3.1](#objects--no-new) Use the literal syntax for object creation. eslint: [`no-new-object`](https://eslint.org/docs/rules/no-new-object)
165
+
166
+ ```javascript static
167
+ // bad
168
+ const item = new Object();
169
+
170
+ // good
171
+ const item = {};
172
+ ```
173
+
174
+ <a name="es6-computed-properties"></a><a name="3.4"></a>
175
+
176
+ - [3.2](#es6-computed-properties) Use computed property names when creating objects with dynamic property names.
177
+
178
+ > Why? They allow you to define all the properties of an object in one place.
179
+
180
+ ```javascript static
181
+ function getKey(k) {
182
+ return `a key named ${k}`;
183
+ }
184
+
185
+ // bad
186
+ const obj = {
187
+ id: 5,
188
+ name: "San Francisco",
189
+ };
190
+ obj[getKey("enabled")] = true;
191
+
192
+ // good
193
+ const obj = {
194
+ id: 5,
195
+ name: "San Francisco",
196
+ [getKey("enabled")]: true,
197
+ };
198
+ ```
199
+
200
+ <a name="es6-object-shorthand"></a><a name="3.5"></a>
201
+
202
+ - [3.3](#es6-object-shorthand) Use object method shorthand. eslint: [`object-shorthand`](https://eslint.org/docs/rules/object-shorthand)
203
+
204
+ ```javascript static
205
+ // bad
206
+ const atom = {
207
+ value: 1,
208
+
209
+ addValue: function (value) {
210
+ return atom.value + value;
211
+ },
212
+ };
213
+
214
+ // good
215
+ const atom = {
216
+ value: 1,
217
+
218
+ addValue(value) {
219
+ return atom.value + value;
220
+ },
221
+ };
222
+ ```
223
+
224
+ <a name="es6-object-concise"></a><a name="3.6"></a>
225
+
226
+ - [3.4](#es6-object-concise) Use property value shorthand. eslint: [`object-shorthand`](https://eslint.org/docs/rules/object-shorthand)
227
+
228
+ > Why? It is shorter and descriptive.
229
+
230
+ ```javascript static
231
+ const lukeSkywalker = "Luke Skywalker";
232
+
233
+ // bad
234
+ const obj = {
235
+ lukeSkywalker: lukeSkywalker,
236
+ };
237
+
238
+ // good
239
+ const obj = {
240
+ lukeSkywalker,
241
+ };
242
+ ```
243
+
244
+ <a name="objects--grouped-shorthand"></a><a name="3.7"></a>
245
+
246
+ - [3.5](#objects--grouped-shorthand) Group your shorthand properties at the beginning of your object declaration.
247
+
248
+ > Why? It’s easier to tell which properties are using the shorthand.
249
+
250
+ ```javascript static
251
+ const anakinSkywalker = "Anakin Skywalker";
252
+ const lukeSkywalker = "Luke Skywalker";
253
+
254
+ // bad
255
+ const obj = {
256
+ episodeOne: 1,
257
+ twoJediWalkIntoACantina: 2,
258
+ lukeSkywalker,
259
+ episodeThree: 3,
260
+ mayTheFourth: 4,
261
+ anakinSkywalker,
262
+ };
263
+
264
+ // good
265
+ const obj = {
266
+ lukeSkywalker,
267
+ anakinSkywalker,
268
+ episodeOne: 1,
269
+ twoJediWalkIntoACantina: 2,
270
+ episodeThree: 3,
271
+ mayTheFourth: 4,
272
+ };
273
+ ```
274
+
275
+ <a name="objects--quoted-props"></a><a name="3.8"></a>
276
+
277
+ - [3.6](#objects--quoted-props) Only quote properties that are invalid identifiers. eslint: [`quote-props`](https://eslint.org/docs/rules/quote-props)
278
+
279
+ > Why? In general we consider it subjectively easier to read. It improves syntax highlighting, and is also more easily optimized by many JS engines.
280
+
281
+ ```javascript static
282
+ // bad
283
+ const bad = {
284
+ foo: 3,
285
+ bar: 4,
286
+ "data-blah": 5,
287
+ };
288
+
289
+ // good
290
+ const good = {
291
+ foo: 3,
292
+ bar: 4,
293
+ "data-blah": 5,
294
+ };
295
+ ```
296
+
297
+ <a name="objects--prototype-builtins"></a>
298
+
299
+ - [3.7](#objects--prototype-builtins) Do not call `Object.prototype` methods directly, such as `hasOwnProperty`, `propertyIsEnumerable`, and `isPrototypeOf`. eslint: [`no-prototype-builtins`](https://eslint.org/docs/rules/no-prototype-builtins)
300
+
301
+ > Why? These methods may be shadowed by properties on the object in question - consider `{ hasOwnProperty: false }` - or, the object may be a null object (`Object.create(null)`).
302
+
303
+ ```javascript static
304
+ // bad
305
+ console.log(object.hasOwnProperty(key));
306
+
307
+ // good
308
+ console.log(Object.prototype.hasOwnProperty.call(object, key));
309
+
310
+ // best
311
+ const has = Object.prototype.hasOwnProperty; // cache the lookup once, in module scope.
312
+ console.log(has.call(object, key));
313
+ /* or */
314
+ import has from "has"; // https://www.npmjs.com/package/has
315
+ console.log(has(object, key));
316
+ /* or */
317
+ console.log(Object.hasOwn(object, key)); // https://www.npmjs.com/package/object.hasown
318
+ ```
319
+
320
+ <a name="objects--rest-spread"></a>
321
+
322
+ - [3.8](#objects--rest-spread) Prefer the object spread syntax over [`Object.assign`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/assign) to shallow-copy objects. Use the object rest parameter syntax to get a new object with certain properties omitted. eslint: [`prefer-object-spread`](https://eslint.org/docs/rules/prefer-object-spread)
323
+
324
+ ```javascript static
325
+ // very bad
326
+ const original = { a: 1, b: 2 };
327
+ const copy = Object.assign(original, { c: 3 }); // this mutates `original` ಠ_ಠ
328
+ delete copy.a; // so does this
329
+
330
+ // bad
331
+ const original = { a: 1, b: 2 };
332
+ const copy = Object.assign({}, original, { c: 3 }); // copy => { a: 1, b: 2, c: 3 }
333
+
334
+ // good
335
+ const original = { a: 1, b: 2 };
336
+ const copy = { ...original, c: 3 }; // copy => { a: 1, b: 2, c: 3 }
337
+
338
+ const { a, ...noA } = copy; // noA => { b: 2, c: 3 }
339
+ ```
340
+
341
+ **[⬆ back to top](#table-of-contents)**
342
+
343
+ ## Arrays
344
+
345
+ <a name="arrays--literals"></a><a name="4.1"></a>
346
+
347
+ - [4.1](#arrays--literals) Use the literal syntax for array creation. eslint: [`no-array-constructor`](https://eslint.org/docs/rules/no-array-constructor)
348
+
349
+ ```javascript static
350
+ // bad
351
+ const items = new Array();
352
+
353
+ // good
354
+ const items = [];
355
+ ```
356
+
357
+ <a name="arrays--push"></a><a name="4.2"></a>
358
+
359
+ - [4.2](#arrays--push) Use [Array#push](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/push) instead of direct assignment to add items to an array.
360
+
361
+ ```javascript static
362
+ const someStack = [];
363
+
364
+ // bad
365
+ someStack[someStack.length] = "abracadabra";
366
+
367
+ // good
368
+ someStack.push("abracadabra");
369
+ ```
370
+
371
+ <a name="es6-array-spreads"></a><a name="4.3"></a>
372
+
373
+ - [4.3](#es6-array-spreads) Use array spreads `...` to copy arrays.
374
+
375
+ ```javascript static
376
+ // bad
377
+ const len = items.length;
378
+ const itemsCopy = [];
379
+ let i;
380
+
381
+ for (i = 0; i < len; i += 1) {
382
+ itemsCopy[i] = items[i];
383
+ }
384
+
385
+ // good
386
+ const itemsCopy = [...items];
387
+ ```
388
+
389
+ <a name="arrays--from"></a>
390
+ <a name="arrays--from-iterable"></a><a name="4.4"></a>
391
+
392
+ - [4.4](#arrays--from-iterable) To convert an iterable object to an array, use spreads `...` instead of [`Array.from`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/from)
393
+
394
+ ```javascript static
395
+ const foo = document.querySelectorAll(".foo");
396
+
397
+ // good
398
+ const nodes = Array.from(foo);
399
+
400
+ // best
401
+ const nodes = [...foo];
402
+ ```
403
+
404
+ <a name="arrays--from-array-like"></a>
405
+
406
+ - [4.5](#arrays--from-array-like) Use [`Array.from`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/from) for converting an array-like object to an array.
407
+
408
+ ```javascript static
409
+ const arrLike = { 0: "foo", 1: "bar", 2: "baz", length: 3 };
410
+
411
+ // bad
412
+ const arr = Array.prototype.slice.call(arrLike);
413
+
414
+ // good
415
+ const arr = Array.from(arrLike);
416
+ ```
417
+
418
+ <a name="arrays--mapping"></a>
419
+
420
+ - [4.6](#arrays--mapping) Use [`Array.from`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/from) instead of spread `...` for mapping over iterables, because it avoids creating an intermediate array.
421
+
422
+ ```javascript static
423
+ // bad
424
+ const baz = [...foo].map(bar);
425
+
426
+ // good
427
+ const baz = Array.from(foo, bar);
428
+ ```
429
+
430
+ <a name="arrays--callback-return"></a><a name="4.5"></a>
431
+
432
+ - [4.7](#arrays--callback-return) Use return statements in array method callbacks. It’s ok to omit the return if the function body consists of a single statement returning an expression without side effects, following [8.2](#arrows--implicit-return). eslint: [`array-callback-return`](https://eslint.org/docs/rules/array-callback-return)
433
+
434
+ ```javascript static
435
+ // good
436
+ [1, 2, 3].map((x) => {
437
+ const y = x + 1;
438
+ return x * y;
439
+ });
440
+
441
+ // good
442
+ [1, 2, 3].map((x) => x + 1);
443
+
444
+ // bad - no returned value means `acc` becomes undefined after the first iteration
445
+ [
446
+ [0, 1],
447
+ [2, 3],
448
+ [4, 5],
449
+ ].reduce((acc, item, index) => {
450
+ const flatten = acc.concat(item);
451
+ });
452
+
453
+ // good
454
+ [
455
+ [0, 1],
456
+ [2, 3],
457
+ [4, 5],
458
+ ].reduce((acc, item, index) => {
459
+ const flatten = acc.concat(item);
460
+ return flatten;
461
+ });
462
+
463
+ // bad
464
+ inbox.filter((msg) => {
465
+ const { subject, author } = msg;
466
+ if (subject === "Mockingbird") {
467
+ return author === "Harper Lee";
468
+ } else {
469
+ return false;
470
+ }
471
+ });
472
+
473
+ // good
474
+ inbox.filter((msg) => {
475
+ const { subject, author } = msg;
476
+ if (subject === "Mockingbird") {
477
+ return author === "Harper Lee";
478
+ }
479
+
480
+ return false;
481
+ });
482
+ ```
483
+
484
+ <a name="arrays--bracket-newline"></a>
485
+
486
+ - [4.8](#arrays--bracket-newline) Use line breaks after open and before close array brackets if an array has multiple lines
487
+
488
+ ```javascript static
489
+ // bad
490
+ const arr = [
491
+ [0, 1],
492
+ [2, 3],
493
+ [4, 5],
494
+ ];
495
+
496
+ const objectInArray = [
497
+ {
498
+ id: 1,
499
+ },
500
+ {
501
+ id: 2,
502
+ },
503
+ ];
504
+
505
+ const numberInArray = [1, 2];
506
+
507
+ // good
508
+ const arr = [
509
+ [0, 1],
510
+ [2, 3],
511
+ [4, 5],
512
+ ];
513
+
514
+ const objectInArray = [
515
+ {
516
+ id: 1,
517
+ },
518
+ {
519
+ id: 2,
520
+ },
521
+ ];
522
+
523
+ const numberInArray = [1, 2];
524
+ ```
525
+
526
+ **[⬆ back to top](#table-of-contents)**
527
+
528
+ ## Destructuring
529
+
530
+ <a name="destructuring--object"></a><a name="5.1"></a>
531
+
532
+ - [5.1](#destructuring--object) Use object destructuring when accessing and using multiple properties of an object. eslint: [`prefer-destructuring`](https://eslint.org/docs/rules/prefer-destructuring)
533
+
534
+ > Why? Destructuring saves you from creating temporary references for those properties, and from repetitive access of the object. Repeating object access creates more repetitive code, requires more reading, and creates more opportunities for mistakes. Destructuring objects also provides a single site of definition of the object structure that is used in the block, rather than requiring reading the entire block to determine what is used.
535
+
536
+ ```javascript static
537
+ // bad
538
+ function getFullName(user) {
539
+ const firstName = user.firstName;
540
+ const lastName = user.lastName;
541
+
542
+ return `${firstName} ${lastName}`;
543
+ }
544
+
545
+ // good
546
+ function getFullName(user) {
547
+ const { firstName, lastName } = user;
548
+ return `${firstName} ${lastName}`;
549
+ }
550
+
551
+ // best
552
+ function getFullName({ firstName, lastName }) {
553
+ return `${firstName} ${lastName}`;
554
+ }
555
+ ```
556
+
557
+ <a name="destructuring--array"></a><a name="5.2"></a>
558
+
559
+ - [5.2](#destructuring--array) Use array destructuring. eslint: [`prefer-destructuring`](https://eslint.org/docs/rules/prefer-destructuring)
560
+
561
+ ```javascript static
562
+ const arr = [1, 2, 3, 4];
563
+
564
+ // bad
565
+ const first = arr[0];
566
+ const second = arr[1];
567
+
568
+ // good
569
+ const [first, second] = arr;
570
+ ```
571
+
572
+ <a name="destructuring--object-over-array"></a><a name="5.3"></a>
573
+
574
+ - [5.3](#destructuring--object-over-array) Use object destructuring for multiple return values, not array destructuring.
575
+
576
+ > Why? You can add new properties over time or change the order of things without breaking call sites.
577
+
578
+ ```javascript static
579
+ // bad
580
+ function processInput(input) {
581
+ // then a miracle occurs
582
+ return [left, right, top, bottom];
583
+ }
584
+
585
+ // the caller needs to think about the order of return data
586
+ const [left, __, top] = processInput(input);
587
+
588
+ // good
589
+ function processInput(input) {
590
+ // then a miracle occurs
591
+ return { left, right, top, bottom };
592
+ }
593
+
594
+ // the caller selects only the data they need
595
+ const { left, top } = processInput(input);
596
+ ```
597
+
598
+ **[⬆ back to top](#table-of-contents)**
599
+
600
+ ## Strings
601
+
602
+ <a name="strings--quotes"></a><a name="6.1"></a>
603
+
604
+ - [6.1](#strings--quotes) Use single quotes `''` for strings. eslint: [`quotes`](https://eslint.org/docs/rules/quotes)
605
+
606
+ ```javascript static
607
+ // bad
608
+ const name = "Capt. Janeway";
609
+
610
+ // bad - template literals should contain interpolation or newlines
611
+ const name = `Capt. Janeway`;
612
+
613
+ // good
614
+ const name = "Capt. Janeway";
615
+ ```
616
+
617
+ <a name="strings--line-length"></a><a name="6.2"></a>
618
+
619
+ - [6.2](#strings--line-length) Strings that cause the line to go over 100 characters should not be written across multiple lines using string concatenation.
620
+
621
+ > Why? Broken strings are painful to work with and make code less searchable.
622
+
623
+ ```javascript static
624
+ // bad
625
+ const errorMessage =
626
+ "This is a super long error that was thrown because \
627
+ of Batman. When you stop to think about how Batman had anything to do \
628
+ with this, you would get nowhere \
629
+ fast.";
630
+
631
+ // bad
632
+ const errorMessage =
633
+ "This is a super long error that was thrown because " +
634
+ "of Batman. When you stop to think about how Batman had anything to do " +
635
+ "with this, you would get nowhere fast.";
636
+
637
+ // good
638
+ const errorMessage =
639
+ "This is a super long error that was thrown because of Batman. When you stop to think about how Batman had anything to do with this, you would get nowhere fast.";
640
+ ```
641
+
642
+ <a name="es6-template-literals"></a><a name="6.4"></a>
643
+
644
+ - [6.3](#es6-template-literals) When programmatically building up strings, use template strings instead of concatenation. eslint: [`prefer-template`](https://eslint.org/docs/rules/prefer-template) [`template-curly-spacing`](https://eslint.org/docs/rules/template-curly-spacing)
645
+
646
+ > Why? Template strings give you a readable, concise syntax with proper newlines and string interpolation features.
647
+
648
+ ```javascript static
649
+ // bad
650
+ function sayHi(name) {
651
+ return "How are you, " + name + "?";
652
+ }
653
+
654
+ // bad
655
+ function sayHi(name) {
656
+ return ["How are you, ", name, "?"].join();
657
+ }
658
+
659
+ // bad
660
+ function sayHi(name) {
661
+ return `How are you, ${name}?`;
662
+ }
663
+
664
+ // good
665
+ function sayHi(name) {
666
+ return `How are you, ${name}?`;
667
+ }
668
+ ```
669
+
670
+ <a name="strings--eval"></a><a name="6.5"></a>
671
+
672
+ - [6.4](#strings--eval) Never use `eval()` on a string, it opens too many vulnerabilities. eslint: [`no-eval`](https://eslint.org/docs/rules/no-eval)
673
+
674
+ <a name="strings--escaping"></a>
675
+
676
+ - [6.5](#strings--escaping) Do not unnecessarily escape characters in strings. eslint: [`no-useless-escape`](https://eslint.org/docs/rules/no-useless-escape)
677
+
678
+ > Why? Backslashes harm readability, thus they should only be present when necessary.
679
+
680
+ ```javascript static
681
+ // bad
682
+ const foo = "'this' is \"quoted\"";
683
+
684
+ // good
685
+ const foo = "'this' is \"quoted\"";
686
+ const foo = `my name is '${name}'`;
687
+ ```
688
+
689
+ **[⬆ back to top](#table-of-contents)**
690
+
691
+ ## Functions
692
+
693
+ <a name="functions--declarations"></a><a name="7.1"></a>
694
+
695
+ - [7.1](#functions--declarations) Use named function expressions instead of function declarations. eslint: [`func-style`](https://eslint.org/docs/rules/func-style)
696
+
697
+ > Why? Function declarations are hoisted, which means that it’s easy - too easy - to reference the function before it is defined in the file. This harms readability and maintainability. If you find that a function’s definition is large or complex enough that it is interfering with understanding the rest of the file, then perhaps it’s time to extract it to its own module! Don’t forget to explicitly name the expression, regardless of whether or not the name is inferred from the containing variable (which is often the case in modern browsers or when using compilers such as Babel). This eliminates any assumptions made about the Error’s call stack. ([Discussion](https://github.com/airbnb/javascript/issues/794))
698
+
699
+ ```javascript static
700
+ // bad
701
+ function foo() {
702
+ // ...
703
+ }
704
+
705
+ // bad
706
+ const foo = function () {
707
+ // ...
708
+ };
709
+
710
+ // good
711
+ // lexical name distinguished from the variable-referenced invocation(s)
712
+ const short = function longUniqueMoreDescriptiveLexicalFoo() {
713
+ // ...
714
+ };
715
+ ```
716
+
717
+ <a name="functions--iife"></a><a name="7.2"></a>
718
+
719
+ - [7.2](#functions--iife) Wrap immediately invoked function expressions in parentheses. eslint: [`wrap-iife`](https://eslint.org/docs/rules/wrap-iife)
720
+
721
+ > Why? An immediately invoked function expression is a single unit - wrapping both it, and its invocation parens, in parens, cleanly expresses this. Note that in a world with modules everywhere, you almost never need an IIFE.
722
+
723
+ ```javascript static
724
+ // immediately-invoked function expression (IIFE)
725
+ (function () {
726
+ console.log("Welcome to the Internet. Please follow me.");
727
+ })();
728
+ ```
729
+
730
+ <a name="functions--in-blocks"></a><a name="7.3"></a>
731
+
732
+ - [7.3](#functions--in-blocks) Never declare a function in a non-function block (`if`, `while`, etc). Assign the function to a variable instead. Browsers will allow you to do it, but they all interpret it differently, which is bad news bears. eslint: [`no-loop-func`](https://eslint.org/docs/rules/no-loop-func)
733
+
734
+ <a name="functions--note-on-blocks"></a><a name="7.4"></a>
735
+
736
+ - [7.4](#functions--note-on-blocks) **Note:** ECMA-262 defines a `block` as a list of statements. A function declaration is not a statement.
737
+
738
+ ```javascript static
739
+ // bad
740
+ if (currentUser) {
741
+ function test() {
742
+ console.log("Nope.");
743
+ }
744
+ }
745
+
746
+ // good
747
+ let test;
748
+ if (currentUser) {
749
+ test = () => {
750
+ console.log("Yup.");
751
+ };
752
+ }
753
+ ```
754
+
755
+ <a name="functions--arguments-shadow"></a><a name="7.5"></a>
756
+
757
+ - [7.5](#functions--arguments-shadow) Never name a parameter `arguments`. This will take precedence over the `arguments` object that is given to every function scope.
758
+
759
+ ```javascript static
760
+ // bad
761
+ function foo(name, options, arguments) {
762
+ // ...
763
+ }
764
+
765
+ // good
766
+ function foo(name, options, args) {
767
+ // ...
768
+ }
769
+ ```
770
+
771
+ <a name="es6-rest"></a><a name="7.6"></a>
772
+
773
+ - [7.6](#es6-rest) Never use `arguments`, opt to use rest syntax `...` instead. eslint: [`prefer-rest-params`](https://eslint.org/docs/rules/prefer-rest-params)
774
+
775
+ > Why? `...` is explicit about which arguments you want pulled. Plus, rest arguments are a real Array, and not merely Array-like like `arguments`.
776
+
777
+ ```javascript static
778
+ // bad
779
+ function concatenateAll() {
780
+ const args = Array.prototype.slice.call(arguments);
781
+ return args.join("");
782
+ }
783
+
784
+ // good
785
+ function concatenateAll(...args) {
786
+ return args.join("");
787
+ }
788
+ ```
789
+
790
+ <a name="es6-default-parameters"></a><a name="7.7"></a>
791
+
792
+ - [7.7](#es6-default-parameters) Use default parameter syntax rather than mutating function arguments.
793
+
794
+ ```javascript static
795
+ // really bad
796
+ function handleThings(opts) {
797
+ // No! We shouldn’t mutate function arguments.
798
+ // Double bad: if opts is falsy it'll be set to an object which may
799
+ // be what you want but it can introduce subtle bugs.
800
+ opts = opts || {};
801
+ // ...
802
+ }
803
+
804
+ // still bad
805
+ function handleThings(opts) {
806
+ if (opts === void 0) {
807
+ opts = {};
808
+ }
809
+ // ...
810
+ }
811
+
812
+ // good
813
+ function handleThings(opts = {}) {
814
+ // ...
815
+ }
816
+ ```
817
+
818
+ <a name="functions--default-side-effects"></a><a name="7.8"></a>
819
+
820
+ - [7.8](#functions--default-side-effects) Avoid side effects with default parameters.
821
+
822
+ > Why? They are confusing to reason about.
823
+
824
+ ```javascript static
825
+ let b = 1;
826
+ // bad
827
+ function count(a = b++) {
828
+ console.log(a);
829
+ }
830
+ count(); // 1
831
+ count(); // 2
832
+ count(3); // 3
833
+ count(); // 3
834
+ ```
835
+
836
+ <a name="functions--defaults-last"></a><a name="7.9"></a>
837
+
838
+ - [7.9](#functions--defaults-last) Always put default parameters last. eslint: [`default-param-last`](https://eslint.org/docs/rules/default-param-last)
839
+
840
+ ```javascript static
841
+ // bad
842
+ function handleThings(opts = {}, name) {
843
+ // ...
844
+ }
845
+
846
+ // good
847
+ function handleThings(name, opts = {}) {
848
+ // ...
849
+ }
850
+ ```
851
+
852
+ <a name="functions--constructor"></a><a name="7.10"></a>
853
+
854
+ - [7.10](#functions--constructor) Never use the Function constructor to create a new function. eslint: [`no-new-func`](https://eslint.org/docs/rules/no-new-func)
855
+
856
+ > Why? Creating a function in this way evaluates a string similarly to `eval()`, which opens vulnerabilities.
857
+
858
+ ```javascript static
859
+ // bad
860
+ const add = new Function("a", "b", "return a + b");
861
+
862
+ // still bad
863
+ const subtract = Function("a", "b", "return a - b");
864
+ ```
865
+
866
+ <a name="functions--signature-spacing"></a><a name="7.11"></a>
867
+
868
+ - [7.11](#functions--signature-spacing) Spacing in a function signature. eslint: [`space-before-function-paren`](https://eslint.org/docs/rules/space-before-function-paren) [`space-before-blocks`](https://eslint.org/docs/rules/space-before-blocks)
869
+
870
+ > Why? Consistency is good, and you shouldn’t have to add or remove a space when adding or removing a name.
871
+
872
+ ```javascript static
873
+ // bad
874
+ const f = function () {};
875
+ const g = function () {};
876
+ const h = function () {};
877
+
878
+ // good
879
+ const x = function () {};
880
+ const y = function a() {};
881
+ ```
882
+
883
+ <a name="functions--mutate-params"></a><a name="7.12"></a>
884
+
885
+ - [7.12](#functions--mutate-params) Never mutate parameters. eslint: [`no-param-reassign`](https://eslint.org/docs/rules/no-param-reassign)
886
+
887
+ > Why? Manipulating objects passed in as parameters can cause unwanted variable side effects in the original caller.
888
+
889
+ ```javascript static
890
+ // bad
891
+ function f1(obj) {
892
+ obj.key = 1;
893
+ }
894
+
895
+ // good
896
+ function f2(obj) {
897
+ const key = Object.prototype.hasOwnProperty.call(obj, "key") ? obj.key : 1;
898
+ }
899
+ ```
900
+
901
+ <a name="functions--reassign-params"></a><a name="7.13"></a>
902
+
903
+ - [7.13](#functions--reassign-params) Never reassign parameters. eslint: [`no-param-reassign`](https://eslint.org/docs/rules/no-param-reassign)
904
+
905
+ > Why? Reassigning parameters can lead to unexpected behavior, especially when accessing the `arguments` object. It can also cause optimization issues, especially in V8.
906
+
907
+ ```javascript static
908
+ // bad
909
+ function f1(a) {
910
+ a = 1;
911
+ // ...
912
+ }
913
+
914
+ function f2(a) {
915
+ if (!a) {
916
+ a = 1;
917
+ }
918
+ // ...
919
+ }
920
+
921
+ // good
922
+ function f3(a) {
923
+ const b = a || 1;
924
+ // ...
925
+ }
926
+
927
+ function f4(a = 1) {
928
+ // ...
929
+ }
930
+ ```
931
+
932
+ <a name="functions--spread-vs-apply"></a><a name="7.14"></a>
933
+
934
+ - [7.14](#functions--spread-vs-apply) Prefer the use of the spread syntax `...` to call variadic functions. eslint: [`prefer-spread`](https://eslint.org/docs/rules/prefer-spread)
935
+
936
+ > Why? It’s cleaner, you don’t need to supply a context, and you can not easily compose `new` with `apply`.
937
+
938
+ ```javascript static
939
+ // bad
940
+ const x = [1, 2, 3, 4, 5];
941
+ console.log.apply(console, x);
942
+
943
+ // good
944
+ const x = [1, 2, 3, 4, 5];
945
+ console.log(...x);
946
+
947
+ // bad
948
+ new (Function.prototype.bind.apply(Date, [null, 2016, 8, 5]))();
949
+
950
+ // good
951
+ new Date(...[2016, 8, 5]);
952
+ ```
953
+
954
+ <a name="functions--signature-invocation-indentation"></a>
955
+
956
+ - [7.15](#functions--signature-invocation-indentation) Functions with multiline signatures, or invocations, should be indented just like every other multiline list in this guide: with each item on a line by itself, with a trailing comma on the last item. eslint: [`function-paren-newline`](https://eslint.org/docs/rules/function-paren-newline)
957
+
958
+ ```javascript static
959
+ // bad
960
+ function foo(bar, baz, quux) {
961
+ // ...
962
+ }
963
+
964
+ // good
965
+ function foo(bar, baz, quux) {
966
+ // ...
967
+ }
968
+
969
+ // bad
970
+ console.log(foo, bar, baz);
971
+
972
+ // good
973
+ console.log(foo, bar, baz);
974
+ ```
975
+
976
+ **[⬆ back to top](#table-of-contents)**
977
+
978
+ ## Arrow Functions
979
+
980
+ <a name="arrows--use-them"></a><a name="8.1"></a>
981
+
982
+ - [8.1](#arrows--use-them) When you must use an anonymous function (as when passing an inline callback), use arrow function notation. eslint: [`prefer-arrow-callback`](https://eslint.org/docs/rules/prefer-arrow-callback), [`arrow-spacing`](https://eslint.org/docs/rules/arrow-spacing)
983
+
984
+ > Why? It creates a version of the function that executes in the context of `this`, which is usually what you want, and is a more concise syntax.
985
+
986
+ > Why not? If you have a fairly complicated function, you might move that logic out into its own named function expression.
987
+
988
+ ```javascript static
989
+ // bad
990
+ [1, 2, 3].map(function (x) {
991
+ const y = x + 1;
992
+ return x * y;
993
+ });
994
+
995
+ // good
996
+ [1, 2, 3].map((x) => {
997
+ const y = x + 1;
998
+ return x * y;
999
+ });
1000
+ ```
1001
+
1002
+ <a name="arrows--implicit-return"></a><a name="8.2"></a>
1003
+
1004
+ - [8.2](#arrows--implicit-return) If the function body consists of a single statement returning an [expression](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators#Expressions) without side effects, omit the braces and use the implicit return. Otherwise, keep the braces and use a `return` statement. eslint: [`arrow-parens`](https://eslint.org/docs/rules/arrow-parens), [`arrow-body-style`](https://eslint.org/docs/rules/arrow-body-style)
1005
+
1006
+ > Why? Syntactic sugar. It reads well when multiple functions are chained together.
1007
+
1008
+ ```javascript static
1009
+ // bad
1010
+ [1, 2, 3].map((number) => {
1011
+ const nextNumber = number + 1;
1012
+ `A string containing the ${nextNumber}.`;
1013
+ });
1014
+
1015
+ // good
1016
+ [1, 2, 3].map((number) => `A string containing the ${number + 1}.`);
1017
+
1018
+ // good
1019
+ [1, 2, 3].map((number) => {
1020
+ const nextNumber = number + 1;
1021
+ return `A string containing the ${nextNumber}.`;
1022
+ });
1023
+
1024
+ // good
1025
+ [1, 2, 3].map((number, index) => ({
1026
+ [index]: number,
1027
+ }));
1028
+
1029
+ // No implicit return with side effects
1030
+ function foo(callback) {
1031
+ const val = callback();
1032
+ if (val === true) {
1033
+ // Do something if callback returns true
1034
+ }
1035
+ }
1036
+
1037
+ let bool = false;
1038
+
1039
+ // bad
1040
+ foo(() => (bool = true));
1041
+
1042
+ // good
1043
+ foo(() => {
1044
+ bool = true;
1045
+ });
1046
+ ```
1047
+
1048
+ <a name="arrows--paren-wrap"></a><a name="8.3"></a>
1049
+
1050
+ - [8.3](#arrows--paren-wrap) In case the expression spans over multiple lines, wrap it in parentheses for better readability.
1051
+
1052
+ > Why? It shows clearly where the function starts and ends.
1053
+
1054
+ ```javascript static
1055
+ // bad
1056
+ ["get", "post", "put"].map((httpMethod) =>
1057
+ Object.prototype.hasOwnProperty.call(
1058
+ httpMagicObjectWithAVeryLongName,
1059
+ httpMethod
1060
+ )
1061
+ );
1062
+
1063
+ // good
1064
+ ["get", "post", "put"].map((httpMethod) =>
1065
+ Object.prototype.hasOwnProperty.call(
1066
+ httpMagicObjectWithAVeryLongName,
1067
+ httpMethod
1068
+ )
1069
+ );
1070
+ ```
1071
+
1072
+ <a name="arrows--one-arg-parens"></a><a name="8.4"></a>
1073
+
1074
+ - [8.4](#arrows--one-arg-parens) Always include parentheses around arguments for clarity and consistency. eslint: [`arrow-parens`](https://eslint.org/docs/rules/arrow-parens)
1075
+
1076
+ > Why? Minimizes diff churn when adding or removing arguments.
1077
+
1078
+ ```javascript static
1079
+ // bad
1080
+ [1, 2, 3].map((x) => x * x);
1081
+
1082
+ // good
1083
+ [1, 2, 3].map((x) => x * x);
1084
+
1085
+ // bad
1086
+ [1, 2, 3].map(
1087
+ (number) =>
1088
+ `A long string with the ${number}. It’s so long that we don’t want it to take up space on the .map line!`
1089
+ );
1090
+
1091
+ // good
1092
+ [1, 2, 3].map(
1093
+ (number) =>
1094
+ `A long string with the ${number}. It’s so long that we don’t want it to take up space on the .map line!`
1095
+ );
1096
+
1097
+ // bad
1098
+ [1, 2, 3].map((x) => {
1099
+ const y = x + 1;
1100
+ return x * y;
1101
+ });
1102
+
1103
+ // good
1104
+ [1, 2, 3].map((x) => {
1105
+ const y = x + 1;
1106
+ return x * y;
1107
+ });
1108
+ ```
1109
+
1110
+ <a name="arrows--confusing"></a><a name="8.5"></a>
1111
+
1112
+ - [8.5](#arrows--confusing) Avoid confusing arrow function syntax (`=>`) with comparison operators (`<=`, `>=`). eslint: [`no-confusing-arrow`](https://eslint.org/docs/rules/no-confusing-arrow)
1113
+
1114
+ ```javascript static
1115
+ // bad
1116
+ const itemHeight = (item) =>
1117
+ item.height <= 256 ? item.largeSize : item.smallSize;
1118
+
1119
+ // bad
1120
+ const itemHeight = (item) =>
1121
+ item.height >= 256 ? item.largeSize : item.smallSize;
1122
+
1123
+ // good
1124
+ const itemHeight = (item) =>
1125
+ item.height <= 256 ? item.largeSize : item.smallSize;
1126
+
1127
+ // good
1128
+ const itemHeight = (item) => {
1129
+ const { height, largeSize, smallSize } = item;
1130
+ return height <= 256 ? largeSize : smallSize;
1131
+ };
1132
+ ```
1133
+
1134
+ <a name="whitespace--implicit-arrow-linebreak"></a>
1135
+
1136
+ - [8.6](#whitespace--implicit-arrow-linebreak) Enforce the location of arrow function bodies with implicit returns. eslint: [`implicit-arrow-linebreak`](https://eslint.org/docs/rules/implicit-arrow-linebreak)
1137
+
1138
+ ```javascript static
1139
+ // bad
1140
+ (foo) => bar;
1141
+
1142
+ (foo) => bar;
1143
+
1144
+ // good
1145
+ (foo) => bar;
1146
+ (foo) => bar;
1147
+ (foo) => bar;
1148
+ ```
1149
+
1150
+ **[⬆ back to top](#table-of-contents)**
1151
+
1152
+ ## Classes & Constructors
1153
+
1154
+ <a name="constructors--use-class"></a><a name="9.1"></a>
1155
+
1156
+ - [9.1](#constructors--use-class) Always use `class`. Avoid manipulating `prototype` directly.
1157
+
1158
+ > Why? `class` syntax is more concise and easier to reason about.
1159
+
1160
+ ```javascript static
1161
+ // bad
1162
+ function Queue(contents = []) {
1163
+ this.queue = [...contents];
1164
+ }
1165
+ Queue.prototype.pop = function () {
1166
+ const value = this.queue[0];
1167
+ this.queue.splice(0, 1);
1168
+ return value;
1169
+ };
1170
+
1171
+ // good
1172
+ class Queue {
1173
+ constructor(contents = []) {
1174
+ this.queue = [...contents];
1175
+ }
1176
+ pop() {
1177
+ const value = this.queue[0];
1178
+ this.queue.splice(0, 1);
1179
+ return value;
1180
+ }
1181
+ }
1182
+ ```
1183
+
1184
+ <a name="constructors--extends"></a><a name="9.2"></a>
1185
+
1186
+ - [9.2](#constructors--extends) Use `extends` for inheritance.
1187
+
1188
+ > Why? It is a built-in way to inherit prototype functionality without breaking `instanceof`.
1189
+
1190
+ ```javascript static
1191
+ // bad
1192
+ const inherits = require("inherits");
1193
+ function PeekableQueue(contents) {
1194
+ Queue.apply(this, contents);
1195
+ }
1196
+ inherits(PeekableQueue, Queue);
1197
+ PeekableQueue.prototype.peek = function () {
1198
+ return this.queue[0];
1199
+ };
1200
+
1201
+ // good
1202
+ class PeekableQueue extends Queue {
1203
+ peek() {
1204
+ return this.queue[0];
1205
+ }
1206
+ }
1207
+ ```
1208
+
1209
+ <a name="constructors--chaining"></a><a name="9.3"></a>
1210
+
1211
+ - [9.3](#constructors--chaining) Methods can return `this` to help with method chaining.
1212
+
1213
+ ```javascript static
1214
+ // bad
1215
+ Jedi.prototype.jump = function () {
1216
+ this.jumping = true;
1217
+ return true;
1218
+ };
1219
+
1220
+ Jedi.prototype.setHeight = function (height) {
1221
+ this.height = height;
1222
+ };
1223
+
1224
+ const luke = new Jedi();
1225
+ luke.jump(); // => true
1226
+ luke.setHeight(20); // => undefined
1227
+
1228
+ // good
1229
+ class Jedi {
1230
+ jump() {
1231
+ this.jumping = true;
1232
+ return this;
1233
+ }
1234
+
1235
+ setHeight(height) {
1236
+ this.height = height;
1237
+ return this;
1238
+ }
1239
+ }
1240
+
1241
+ const luke = new Jedi();
1242
+
1243
+ luke.jump().setHeight(20);
1244
+ ```
1245
+
1246
+ <a name="constructors--tostring"></a><a name="9.4"></a>
1247
+
1248
+ - [9.4](#constructors--tostring) It’s okay to write a custom `toString()` method, just make sure it works successfully and causes no side effects.
1249
+
1250
+ ```javascript static
1251
+ class Jedi {
1252
+ constructor(options = {}) {
1253
+ this.name = options.name || "no name";
1254
+ }
1255
+
1256
+ getName() {
1257
+ return this.name;
1258
+ }
1259
+
1260
+ toString() {
1261
+ return `Jedi - ${this.getName()}`;
1262
+ }
1263
+ }
1264
+ ```
1265
+
1266
+ <a name="constructors--no-useless"></a><a name="9.5"></a>
1267
+
1268
+ - [9.5](#constructors--no-useless) Classes have a default constructor if one is not specified. An empty constructor function or one that just delegates to a parent class is unnecessary. eslint: [`no-useless-constructor`](https://eslint.org/docs/rules/no-useless-constructor)
1269
+
1270
+ ```javascript static
1271
+ // bad
1272
+ class Jedi {
1273
+ constructor() {}
1274
+
1275
+ getName() {
1276
+ return this.name;
1277
+ }
1278
+ }
1279
+
1280
+ // bad
1281
+ class Rey extends Jedi {
1282
+ constructor(...args) {
1283
+ super(...args);
1284
+ }
1285
+ }
1286
+
1287
+ // good
1288
+ class Rey extends Jedi {
1289
+ constructor(...args) {
1290
+ super(...args);
1291
+ this.name = "Rey";
1292
+ }
1293
+ }
1294
+ ```
1295
+
1296
+ <a name="classes--no-duplicate-members"></a>
1297
+
1298
+ - [9.6](#classes--no-duplicate-members) Avoid duplicate class members. eslint: [`no-dupe-class-members`](https://eslint.org/docs/rules/no-dupe-class-members)
1299
+
1300
+ > Why? Duplicate class member declarations will silently prefer the last one - having duplicates is almost certainly a bug.
1301
+
1302
+ ```javascript static
1303
+ // bad
1304
+ class Foo {
1305
+ bar() {
1306
+ return 1;
1307
+ }
1308
+ bar() {
1309
+ return 2;
1310
+ }
1311
+ }
1312
+
1313
+ // good
1314
+ class Foo {
1315
+ bar() {
1316
+ return 1;
1317
+ }
1318
+ }
1319
+
1320
+ // good
1321
+ class Foo {
1322
+ bar() {
1323
+ return 2;
1324
+ }
1325
+ }
1326
+ ```
1327
+
1328
+ <a name="classes--methods-use-this"></a>
1329
+
1330
+ - [9.7](#classes--methods-use-this) Class methods should use `this` or be made into a static method unless an external library or framework requires using specific non-static methods. Being an instance method should indicate that it behaves differently based on properties of the receiver. eslint: [`class-methods-use-this`](https://eslint.org/docs/rules/class-methods-use-this)
1331
+
1332
+ ```javascript static
1333
+ // bad
1334
+ class Foo {
1335
+ bar() {
1336
+ console.log("bar");
1337
+ }
1338
+ }
1339
+
1340
+ // good - this is used
1341
+ class Foo {
1342
+ bar() {
1343
+ console.log(this.bar);
1344
+ }
1345
+ }
1346
+
1347
+ // good - constructor is exempt
1348
+ class Foo {
1349
+ constructor() {
1350
+ // ...
1351
+ }
1352
+ }
1353
+
1354
+ // good - static methods aren't expected to use this
1355
+ class Foo {
1356
+ static bar() {
1357
+ console.log("bar");
1358
+ }
1359
+ }
1360
+ ```
1361
+
1362
+ **[⬆ back to top](#table-of-contents)**
1363
+
1364
+ ## Modules
1365
+
1366
+ <a name="modules--use-them"></a><a name="10.1"></a>
1367
+
1368
+ - [10.1](#modules--use-them) Always use modules (`import`/`export`) over a non-standard module system. You can always transpile to your preferred module system.
1369
+
1370
+ > Why? Modules are the future, let’s start using the future now.
1371
+
1372
+ ```javascript static
1373
+ // bad
1374
+ const AirbnbStyleGuide = require('./AirbnbStyleGuide');
1375
+ module.exports = AirbnbStyleGuide.es6;
1376
+
1377
+ // ok
1378
+ import AirbnbStyleGuide from './AirbnbStyleGuide';
1379
+ export default AirbnbStyleGuide.es6;
1380
+
1381
+ // best
1382
+ import { es6 } from './AirbnbStyleGuide';
1383
+ export default es6;
1384
+ ```
1385
+
1386
+ <a name="modules--no-wildcard"></a><a name="10.2"></a>
1387
+
1388
+ - [10.2](#modules--no-wildcard) Do not use wildcard imports.
1389
+
1390
+ > Why? This makes sure you have a single default export.
1391
+
1392
+ ```javascript static
1393
+ // bad
1394
+ import * as AirbnbStyleGuide from "./AirbnbStyleGuide";
1395
+
1396
+ // good
1397
+ import AirbnbStyleGuide from "./AirbnbStyleGuide";
1398
+ ```
1399
+
1400
+ <a name="modules--no-export-from-import"></a><a name="10.3"></a>
1401
+
1402
+ - [10.3](#modules--no-export-from-import) And do not export directly from an import.
1403
+
1404
+ > Why? Although the one-liner is concise, having one clear way to import and one clear way to export makes things consistent.
1405
+
1406
+ ```javascript static
1407
+ // bad
1408
+ // filename es6.js
1409
+ export { es6 as default } from './AirbnbStyleGuide';
1410
+
1411
+ // good
1412
+ // filename es6.js
1413
+ import { es6 } from './AirbnbStyleGuide';
1414
+ export default es6;
1415
+ ```
1416
+
1417
+ <a name="modules--no-duplicate-imports"></a>
1418
+
1419
+ - [10.4](#modules--no-duplicate-imports) Only import from a path in one place.
1420
+ eslint: [`no-duplicate-imports`](https://eslint.org/docs/rules/no-duplicate-imports)
1421
+
1422
+ > Why? Having multiple lines that import from the same path can make code harder to maintain.
1423
+
1424
+ ```javascript static
1425
+ // bad
1426
+ import foo from "foo";
1427
+ // … some other imports … //
1428
+ import { named1, named2 } from "foo";
1429
+
1430
+ // good
1431
+ import foo, { named1, named2 } from "foo";
1432
+
1433
+ // good
1434
+ import foo, { named1, named2 } from "foo";
1435
+ ```
1436
+
1437
+ <a name="modules--no-mutable-exports"></a>
1438
+
1439
+ - [10.5](#modules--no-mutable-exports) Do not export mutable bindings.
1440
+ eslint: [`import/no-mutable-exports`](https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/no-mutable-exports.md)
1441
+
1442
+ > Why? Mutation should be avoided in general, but in particular when exporting mutable bindings. While this technique may be needed for some special cases, in general, only constant references should be exported.
1443
+
1444
+ ```javascript static
1445
+ // bad
1446
+ let foo = 3;
1447
+ export { foo };
1448
+
1449
+ // good
1450
+ const foo = 3;
1451
+ export { foo };
1452
+ ```
1453
+
1454
+ <a name="modules--prefer-default-export"></a>
1455
+
1456
+ - [10.6](#modules--prefer-default-export) In modules with a single export, prefer default export over named export.
1457
+ eslint: [`import/prefer-default-export`](https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/prefer-default-export.md)
1458
+
1459
+ > Why? To encourage more files that only ever export one thing, which is better for readability and maintainability.
1460
+
1461
+ ```javascript static
1462
+ // bad
1463
+ export function foo() {}
1464
+
1465
+ // good
1466
+ export default function foo() {}
1467
+ ```
1468
+
1469
+ <a name="modules--imports-first"></a>
1470
+
1471
+ - [10.7](#modules--imports-first) Put all `import`s above non-import statements.
1472
+ eslint: [`import/first`](https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/first.md)
1473
+
1474
+ > Why? Since `import`s are hoisted, keeping them all at the top prevents surprising behavior.
1475
+
1476
+ ```javascript static
1477
+ // bad
1478
+ import foo from "foo";
1479
+ foo.init();
1480
+
1481
+ import bar from "bar";
1482
+
1483
+ // good
1484
+ import foo from "foo";
1485
+ import bar from "bar";
1486
+
1487
+ foo.init();
1488
+ ```
1489
+
1490
+ <a name="modules--multiline-imports-over-newlines"></a>
1491
+
1492
+ - [10.8](#modules--multiline-imports-over-newlines) Multiline imports should be indented just like multiline array and object literals.
1493
+ eslint: [`object-curly-newline`](https://eslint.org/docs/rules/object-curly-newline)
1494
+
1495
+ > Why? The curly braces follow the same indentation rules as every other curly brace block in the style guide, as do the trailing commas.
1496
+
1497
+ ```javascript static
1498
+ // bad
1499
+ import { longNameA, longNameB, longNameC, longNameD, longNameE } from "path";
1500
+
1501
+ // good
1502
+ import { longNameA, longNameB, longNameC, longNameD, longNameE } from "path";
1503
+ ```
1504
+
1505
+ <a name="modules--no-webpack-loader-syntax"></a>
1506
+
1507
+ - [10.9](#modules--no-webpack-loader-syntax) Disallow Webpack loader syntax in module import statements.
1508
+ eslint: [`import/no-webpack-loader-syntax`](https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/no-webpack-loader-syntax.md)
1509
+
1510
+ > Why? Since using Webpack syntax in the imports couples the code to a module bundler. Prefer using the loader syntax in `webpack.config.js`.
1511
+
1512
+ ```javascript static
1513
+ // bad
1514
+ import fooSass from "css!sass!foo.scss";
1515
+ import barCss from "style!css!bar.css";
1516
+
1517
+ // good
1518
+ import fooSass from "foo.scss";
1519
+ import barCss from "bar.css";
1520
+ ```
1521
+
1522
+ <a name="modules--import-extensions"></a>
1523
+
1524
+ - [10.10](#modules--import-extensions) Do not include JavaScript filename extensions
1525
+ eslint: [`import/extensions`](https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/extensions.md)
1526
+
1527
+ > Why? Including extensions inhibits refactoring, and inappropriately hardcodes implementation details of the module you're importing in every consumer.
1528
+
1529
+ ```javascript static
1530
+ // bad
1531
+ import foo from "./foo.js";
1532
+ import bar from "./bar.jsx";
1533
+ import baz from "./baz/index.jsx";
1534
+
1535
+ // good
1536
+ import foo from "./foo";
1537
+ import bar from "./bar";
1538
+ import baz from "./baz";
1539
+ ```
1540
+
1541
+ **[⬆ back to top](#table-of-contents)**
1542
+
1543
+ ## Iterators and Generators
1544
+
1545
+ <a name="iterators--nope"></a><a name="11.1"></a>
1546
+
1547
+ - [11.1](#iterators--nope) Don’t use iterators. Prefer JavaScript’s higher-order functions instead of loops like `for-in` or `for-of`. eslint: [`no-iterator`](https://eslint.org/docs/rules/no-iterator) [`no-restricted-syntax`](https://eslint.org/docs/rules/no-restricted-syntax)
1548
+
1549
+ > Why? This enforces our immutable rule. Dealing with pure functions that return values is easier to reason about than side effects.
1550
+
1551
+ > Use `map()` / `every()` / `filter()` / `find()` / `findIndex()` / `reduce()` / `some()` / ... to iterate over arrays, and `Object.keys()` / `Object.values()` / `Object.entries()` to produce arrays so you can iterate over objects.
1552
+
1553
+ ```javascript static
1554
+ const numbers = [1, 2, 3, 4, 5];
1555
+
1556
+ // bad
1557
+ let sum = 0;
1558
+ for (let num of numbers) {
1559
+ sum += num;
1560
+ }
1561
+ sum === 15;
1562
+
1563
+ // good
1564
+ let sum = 0;
1565
+ numbers.forEach((num) => {
1566
+ sum += num;
1567
+ });
1568
+ sum === 15;
1569
+
1570
+ // best (use the functional force)
1571
+ const sum = numbers.reduce((total, num) => total + num, 0);
1572
+ sum === 15;
1573
+
1574
+ // bad
1575
+ const increasedByOne = [];
1576
+ for (let i = 0; i < numbers.length; i++) {
1577
+ increasedByOne.push(numbers[i] + 1);
1578
+ }
1579
+
1580
+ // good
1581
+ const increasedByOne = [];
1582
+ numbers.forEach((num) => {
1583
+ increasedByOne.push(num + 1);
1584
+ });
1585
+
1586
+ // best (keeping it functional)
1587
+ const increasedByOne = numbers.map((num) => num + 1);
1588
+ ```
1589
+
1590
+ <a name="generators--nope"></a><a name="11.2"></a>
1591
+
1592
+ - [11.2](#generators--nope) Don’t use generators for now.
1593
+
1594
+ > Why? They don’t transpile well to ES5.
1595
+
1596
+ <a name="generators--spacing"></a>
1597
+
1598
+ - [11.3](#generators--spacing) If you must use generators, or if you disregard [our advice](#generators--nope), make sure their function signature is spaced properly. eslint: [`generator-star-spacing`](https://eslint.org/docs/rules/generator-star-spacing)
1599
+
1600
+ > Why? `function` and `*` are part of the same conceptual keyword - `*` is not a modifier for `function`, `function*` is a unique construct, different from `function`.
1601
+
1602
+ ```javascript static
1603
+ // bad
1604
+ function* foo() {
1605
+ // ...
1606
+ }
1607
+
1608
+ // bad
1609
+ const bar = function* () {
1610
+ // ...
1611
+ };
1612
+
1613
+ // bad
1614
+ const baz = function* () {
1615
+ // ...
1616
+ };
1617
+
1618
+ // bad
1619
+ const quux = function* () {
1620
+ // ...
1621
+ };
1622
+
1623
+ // bad
1624
+ function* foo() {
1625
+ // ...
1626
+ }
1627
+
1628
+ // bad
1629
+ function* foo() {
1630
+ // ...
1631
+ }
1632
+
1633
+ // very bad
1634
+ function* foo() {
1635
+ // ...
1636
+ }
1637
+
1638
+ // very bad
1639
+ const wat = function* () {
1640
+ // ...
1641
+ };
1642
+
1643
+ // good
1644
+ function* foo() {
1645
+ // ...
1646
+ }
1647
+
1648
+ // good
1649
+ const foo = function* () {
1650
+ // ...
1651
+ };
1652
+ ```
1653
+
1654
+ **[⬆ back to top](#table-of-contents)**
1655
+
1656
+ ## Properties
1657
+
1658
+ <a name="properties--dot"></a><a name="12.1"></a>
1659
+
1660
+ - [12.1](#properties--dot) Use dot notation when accessing properties. eslint: [`dot-notation`](https://eslint.org/docs/rules/dot-notation)
1661
+
1662
+ ```javascript static
1663
+ const luke = {
1664
+ jedi: true,
1665
+ age: 28,
1666
+ };
1667
+
1668
+ // bad
1669
+ const isJedi = luke["jedi"];
1670
+
1671
+ // good
1672
+ const isJedi = luke.jedi;
1673
+ ```
1674
+
1675
+ <a name="properties--bracket"></a><a name="12.2"></a>
1676
+
1677
+ - [12.2](#properties--bracket) Use bracket notation `[]` when accessing properties with a variable.
1678
+
1679
+ ```javascript static
1680
+ const luke = {
1681
+ jedi: true,
1682
+ age: 28,
1683
+ };
1684
+
1685
+ function getProp(prop) {
1686
+ return luke[prop];
1687
+ }
1688
+
1689
+ const isJedi = getProp("jedi");
1690
+ ```
1691
+
1692
+ <a name="es2016-properties--exponentiation-operator"></a>
1693
+
1694
+ - [12.3](#es2016-properties--exponentiation-operator) Use exponentiation operator `**` when calculating exponentiations. eslint: [`no-restricted-properties`](https://eslint.org/docs/rules/no-restricted-properties).
1695
+
1696
+ ```javascript static
1697
+ // bad
1698
+ const binary = Math.pow(2, 10);
1699
+
1700
+ // good
1701
+ const binary = 2 ** 10;
1702
+ ```
1703
+
1704
+ **[⬆ back to top](#table-of-contents)**
1705
+
1706
+ ## Variables
1707
+
1708
+ <a name="variables--const"></a><a name="13.1"></a>
1709
+
1710
+ - [13.1](#variables--const) Always use `const` or `let` to declare variables. Not doing so will result in global variables. We want to avoid polluting the global namespace. Captain Planet warned us of that. eslint: [`no-undef`](https://eslint.org/docs/rules/no-undef) [`prefer-const`](https://eslint.org/docs/rules/prefer-const)
1711
+
1712
+ ```javascript static
1713
+ // bad
1714
+ superPower = new SuperPower();
1715
+
1716
+ // good
1717
+ const superPower = new SuperPower();
1718
+ ```
1719
+
1720
+ <a name="variables--one-const"></a><a name="13.2"></a>
1721
+
1722
+ - [13.2](#variables--one-const) Use one `const` or `let` declaration per variable or assignment. eslint: [`one-var`](https://eslint.org/docs/rules/one-var)
1723
+
1724
+ > Why? It’s easier to add new variable declarations this way, and you never have to worry about swapping out a `;` for a `,` or introducing punctuation-only diffs. You can also step through each declaration with the debugger, instead of jumping through all of them at once.
1725
+
1726
+ ```javascript static
1727
+ // bad
1728
+ const items = getItems(),
1729
+ goSportsTeam = true,
1730
+ dragonball = "z";
1731
+
1732
+ // bad
1733
+ // (compare to above, and try to spot the mistake)
1734
+ const items = getItems(),
1735
+ goSportsTeam = true;
1736
+ dragonball = "z";
1737
+
1738
+ // good
1739
+ const items = getItems();
1740
+ const goSportsTeam = true;
1741
+ const dragonball = "z";
1742
+ ```
1743
+
1744
+ <a name="variables--const-let-group"></a><a name="13.3"></a>
1745
+
1746
+ - [13.3](#variables--const-let-group) Group all your `const`s and then group all your `let`s.
1747
+
1748
+ > Why? This is helpful when later on you might need to assign a variable depending on one of the previously assigned variables.
1749
+
1750
+ ```javascript static
1751
+ // bad
1752
+ let i,
1753
+ len,
1754
+ dragonball,
1755
+ items = getItems(),
1756
+ goSportsTeam = true;
1757
+
1758
+ // bad
1759
+ let i;
1760
+ const items = getItems();
1761
+ let dragonball;
1762
+ const goSportsTeam = true;
1763
+ let len;
1764
+
1765
+ // good
1766
+ const goSportsTeam = true;
1767
+ const items = getItems();
1768
+ let dragonball;
1769
+ let i;
1770
+ let length;
1771
+ ```
1772
+
1773
+ <a name="variables--define-where-used"></a><a name="13.4"></a>
1774
+
1775
+ - [13.4](#variables--define-where-used) Assign variables where you need them, but place them in a reasonable place.
1776
+
1777
+ > Why? `let` and `const` are block scoped and not function scoped.
1778
+
1779
+ ```javascript static
1780
+ // bad - unnecessary function call
1781
+ function checkName(hasName) {
1782
+ const name = getName();
1783
+
1784
+ if (hasName === "test") {
1785
+ return false;
1786
+ }
1787
+
1788
+ if (name === "test") {
1789
+ this.setName("");
1790
+ return false;
1791
+ }
1792
+
1793
+ return name;
1794
+ }
1795
+
1796
+ // good
1797
+ function checkName(hasName) {
1798
+ if (hasName === "test") {
1799
+ return false;
1800
+ }
1801
+
1802
+ const name = getName();
1803
+
1804
+ if (name === "test") {
1805
+ this.setName("");
1806
+ return false;
1807
+ }
1808
+
1809
+ return name;
1810
+ }
1811
+ ```
1812
+
1813
+ <a name="variables--no-chain-assignment"></a><a name="13.5"></a>
1814
+
1815
+ - [13.5](#variables--no-chain-assignment) Don’t chain variable assignments. eslint: [`no-multi-assign`](https://eslint.org/docs/rules/no-multi-assign)
1816
+
1817
+ > Why? Chaining variable assignments creates implicit global variables.
1818
+
1819
+ ```javascript static
1820
+ // bad
1821
+ (function example() {
1822
+ // JavaScript interprets this as
1823
+ // let a = ( b = ( c = 1 ) );
1824
+ // The let keyword only applies to variable a; variables b and c become
1825
+ // global variables.
1826
+ let a = (b = c = 1);
1827
+ })();
1828
+
1829
+ console.log(a); // throws ReferenceError
1830
+ console.log(b); // 1
1831
+ console.log(c); // 1
1832
+
1833
+ // good
1834
+ (function example() {
1835
+ let a = 1;
1836
+ let b = a;
1837
+ let c = a;
1838
+ })();
1839
+
1840
+ console.log(a); // throws ReferenceError
1841
+ console.log(b); // throws ReferenceError
1842
+ console.log(c); // throws ReferenceError
1843
+
1844
+ // the same applies for `const`
1845
+ ```
1846
+
1847
+ <a name="variables--unary-increment-decrement"></a><a name="13.6"></a>
1848
+
1849
+ - [13.6](#variables--unary-increment-decrement) Avoid using unary increments and decrements (`++`, `--`). eslint [`no-plusplus`](https://eslint.org/docs/rules/no-plusplus)
1850
+
1851
+ > Why? Per the eslint documentation, unary increment and decrement statements are subject to automatic semicolon insertion and can cause silent errors with incrementing or decrementing values within an application. It is also more expressive to mutate your values with statements like `num += 1` instead of `num++` or `num ++`. Disallowing unary increment and decrement statements also prevents you from pre-incrementing/pre-decrementing values unintentionally which can also cause unexpected behavior in your programs.
1852
+
1853
+ ```javascript static
1854
+ // bad
1855
+
1856
+ const array = [1, 2, 3];
1857
+ let num = 1;
1858
+ num++;
1859
+ --num;
1860
+
1861
+ let sum = 0;
1862
+ let truthyCount = 0;
1863
+ for (let i = 0; i < array.length; i++) {
1864
+ let value = array[i];
1865
+ sum += value;
1866
+ if (value) {
1867
+ truthyCount++;
1868
+ }
1869
+ }
1870
+
1871
+ // good
1872
+
1873
+ const array = [1, 2, 3];
1874
+ let num = 1;
1875
+ num += 1;
1876
+ num -= 1;
1877
+
1878
+ const sum = array.reduce((a, b) => a + b, 0);
1879
+ const truthyCount = array.filter(Boolean).length;
1880
+ ```
1881
+
1882
+ <a name="variables--linebreak"></a>
1883
+
1884
+ - [13.7](#variables--linebreak) Avoid linebreaks before or after `=` in an assignment. If your assignment violates [`max-len`](https://eslint.org/docs/rules/max-len), surround the value in parens. eslint [`operator-linebreak`](https://eslint.org/docs/rules/operator-linebreak).
1885
+
1886
+ > Why? Linebreaks surrounding `=` can obfuscate the value of an assignment.
1887
+
1888
+ ```javascript static
1889
+ // bad
1890
+ const foo = superLongLongLongLongLongLongLongLongFunctionName();
1891
+
1892
+ // bad
1893
+ const foo = "superLongLongLongLongLongLongLongLongString";
1894
+
1895
+ // good
1896
+ const foo = superLongLongLongLongLongLongLongLongFunctionName();
1897
+
1898
+ // good
1899
+ const foo = "superLongLongLongLongLongLongLongLongString";
1900
+ ```
1901
+
1902
+ <a name="variables--no-unused-vars"></a>
1903
+
1904
+ - [13.8](#variables--no-unused-vars) Disallow unused variables. eslint: [`no-unused-vars`](https://eslint.org/docs/rules/no-unused-vars)
1905
+
1906
+ > Why? Variables that are declared and not used anywhere in the code are most likely an error due to incomplete refactoring. Such variables take up space in the code and can lead to confusion by readers.
1907
+
1908
+ ```javascript static
1909
+ // bad
1910
+
1911
+ const some_unused_var = 42;
1912
+
1913
+ // Write-only variables are not considered as used.
1914
+ let y = 10;
1915
+ y = 5;
1916
+
1917
+ // A read for a modification of itself is not considered as used.
1918
+ let z = 0;
1919
+ z = z + 1;
1920
+
1921
+ // Unused function arguments.
1922
+ function getX(x, y) {
1923
+ return x;
1924
+ }
1925
+
1926
+ // good
1927
+
1928
+ function getXPlusY(x, y) {
1929
+ return x + y;
1930
+ }
1931
+
1932
+ const x = 1;
1933
+ const y = a + 2;
1934
+
1935
+ alert(getXPlusY(x, y));
1936
+
1937
+ // 'type' is ignored even if unused because it has a rest property sibling.
1938
+ // This is a form of extracting an object that omits the specified keys.
1939
+ const { type, ...coords } = data;
1940
+ // 'coords' is now the 'data' object without its 'type' property.
1941
+ ```
1942
+
1943
+ **[⬆ back to top](#table-of-contents)**
1944
+
1945
+ ## Hoisting
1946
+
1947
+ <a name="hoisting--about"></a><a name="14.1"></a>
1948
+
1949
+ - [14.1](#hoisting--about) `var` declarations get hoisted to the top of their closest enclosing function scope, their assignment does not. `const` and `let` declarations are blessed with a new concept called [Temporal Dead Zones (TDZ)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let#temporal_dead_zone_tdz). It’s important to know why [typeof is no longer safe](https://web.archive.org/web/20200121061528/http://es-discourse.com/t/why-typeof-is-no-longer-safe/15).
1950
+
1951
+ ```javascript static
1952
+ // we know this wouldn’t work (assuming there
1953
+ // is no notDefined global variable)
1954
+ function example() {
1955
+ console.log(notDefined); // => throws a ReferenceError
1956
+ }
1957
+
1958
+ // creating a variable declaration after you
1959
+ // reference the variable will work due to
1960
+ // variable hoisting. Note: the assignment
1961
+ // value of `true` is not hoisted.
1962
+ function example() {
1963
+ console.log(declaredButNotAssigned); // => undefined
1964
+ var declaredButNotAssigned = true;
1965
+ }
1966
+
1967
+ // the interpreter is hoisting the variable
1968
+ // declaration to the top of the scope,
1969
+ // which means our example could be rewritten as:
1970
+ function example() {
1971
+ let declaredButNotAssigned;
1972
+ console.log(declaredButNotAssigned); // => undefined
1973
+ declaredButNotAssigned = true;
1974
+ }
1975
+
1976
+ // using const and let
1977
+ function example() {
1978
+ console.log(declaredButNotAssigned); // => throws a ReferenceError
1979
+ console.log(typeof declaredButNotAssigned); // => throws a ReferenceError
1980
+ const declaredButNotAssigned = true;
1981
+ }
1982
+ ```
1983
+
1984
+ <a name="hoisting--anon-expressions"></a><a name="14.2"></a>
1985
+
1986
+ - [14.2](#hoisting--anon-expressions) Anonymous function expressions hoist their variable name, but not the function assignment.
1987
+
1988
+ ```javascript static
1989
+ function example() {
1990
+ console.log(anonymous); // => undefined
1991
+
1992
+ anonymous(); // => TypeError anonymous is not a function
1993
+
1994
+ var anonymous = function () {
1995
+ console.log("anonymous function expression");
1996
+ };
1997
+ }
1998
+ ```
1999
+
2000
+ <a name="hoisting--named-expresions"></a><a name="hoisting--named-expressions"></a><a name="14.3"></a>
2001
+
2002
+ - [14.3](#hoisting--named-expressions) Named function expressions hoist the variable name, not the function name or the function body.
2003
+
2004
+ ```javascript static
2005
+ function example() {
2006
+ console.log(named); // => undefined
2007
+
2008
+ named(); // => TypeError named is not a function
2009
+
2010
+ superPower(); // => ReferenceError superPower is not defined
2011
+
2012
+ var named = function superPower() {
2013
+ console.log("Flying");
2014
+ };
2015
+ }
2016
+
2017
+ // the same is true when the function name
2018
+ // is the same as the variable name.
2019
+ function example() {
2020
+ console.log(named); // => undefined
2021
+
2022
+ named(); // => TypeError named is not a function
2023
+
2024
+ var named = function named() {
2025
+ console.log("named");
2026
+ };
2027
+ }
2028
+ ```
2029
+
2030
+ <a name="hoisting--declarations"></a><a name="14.4"></a>
2031
+
2032
+ - [14.4](#hoisting--declarations) Function declarations hoist their name and the function body.
2033
+
2034
+ ```javascript static
2035
+ function example() {
2036
+ superPower(); // => Flying
2037
+
2038
+ function superPower() {
2039
+ console.log("Flying");
2040
+ }
2041
+ }
2042
+ ```
2043
+
2044
+ - For more information refer to [JavaScript Scoping & Hoisting](https://www.adequatelygood.com/2010/2/JavaScript-Scoping-and-Hoisting/) by [Ben Cherry](https://www.adequatelygood.com/).
2045
+
2046
+ **[⬆ back to top](#table-of-contents)**
2047
+
2048
+ ## Comparison Operators & Equality
2049
+
2050
+ <a name="comparison--eqeqeq"></a><a name="15.1"></a>
2051
+
2052
+ - [15.1](#comparison--eqeqeq) Use `===` and `!==` over `==` and `!=`. eslint: [`eqeqeq`](https://eslint.org/docs/rules/eqeqeq)
2053
+
2054
+ <a name="comparison--if"></a><a name="15.2"></a>
2055
+
2056
+ - [15.2](#comparison--if) Conditional statements such as the `if` statement evaluate their expression using coercion with the `ToBoolean` abstract method and always follow these simple rules:
2057
+
2058
+ - **Objects** evaluate to **true**
2059
+ - **Undefined** evaluates to **false**
2060
+ - **Null** evaluates to **false**
2061
+ - **Booleans** evaluate to **the value of the boolean**
2062
+ - **Numbers** evaluate to **false** if **+0, -0, or NaN**, otherwise **true**
2063
+ - **Strings** evaluate to **false** if an empty string `''`, otherwise **true**
2064
+
2065
+ ```javascript static
2066
+ if ([0] && []) {
2067
+ // true
2068
+ // an array (even an empty one) is an object, objects will evaluate to true
2069
+ }
2070
+ ```
2071
+
2072
+ <a name="comparison--shortcuts"></a><a name="15.3"></a>
2073
+
2074
+ - [15.3](#comparison--shortcuts) Use shortcuts for booleans, but explicit comparisons for strings and numbers.
2075
+
2076
+ ```javascript static
2077
+ // bad
2078
+ if (isValid === true) {
2079
+ // ...
2080
+ }
2081
+
2082
+ // good
2083
+ if (isValid) {
2084
+ // ...
2085
+ }
2086
+
2087
+ // bad
2088
+ if (name) {
2089
+ // ...
2090
+ }
2091
+
2092
+ // good
2093
+ if (name !== "") {
2094
+ // ...
2095
+ }
2096
+
2097
+ // bad
2098
+ if (collection.length) {
2099
+ // ...
2100
+ }
2101
+
2102
+ // good
2103
+ if (collection.length > 0) {
2104
+ // ...
2105
+ }
2106
+ ```
2107
+
2108
+ <a name="comparison--moreinfo"></a><a name="15.4"></a>
2109
+
2110
+ - [15.4](#comparison--moreinfo) For more information see [Truth Equality and JavaScript](https://javascriptweblog.wordpress.com/2011/02/07/truth-equality-and-javascript/#more-2108) by Angus Croll.
2111
+
2112
+ <a name="comparison--switch-blocks"></a><a name="15.5"></a>
2113
+
2114
+ - [15.5](#comparison--switch-blocks) Use braces to create blocks in `case` and `default` clauses that contain lexical declarations (e.g. `let`, `const`, `function`, and `class`). eslint: [`no-case-declarations`](https://eslint.org/docs/rules/no-case-declarations)
2115
+
2116
+ > Why? Lexical declarations are visible in the entire `switch` block but only get initialized when assigned, which only happens when its `case` is reached. This causes problems when multiple `case` clauses attempt to define the same thing.
2117
+
2118
+ ```javascript static
2119
+ // bad
2120
+ switch (foo) {
2121
+ case 1:
2122
+ let x = 1;
2123
+ break;
2124
+ case 2:
2125
+ const y = 2;
2126
+ break;
2127
+ case 3:
2128
+ function f() {
2129
+ // ...
2130
+ }
2131
+ break;
2132
+ default:
2133
+ class C {}
2134
+ }
2135
+
2136
+ // good
2137
+ switch (foo) {
2138
+ case 1: {
2139
+ let x = 1;
2140
+ break;
2141
+ }
2142
+ case 2: {
2143
+ const y = 2;
2144
+ break;
2145
+ }
2146
+ case 3: {
2147
+ function f() {
2148
+ // ...
2149
+ }
2150
+ break;
2151
+ }
2152
+ case 4:
2153
+ bar();
2154
+ break;
2155
+ default: {
2156
+ class C {}
2157
+ }
2158
+ }
2159
+ ```
2160
+
2161
+ <a name="comparison--nested-ternaries"></a><a name="15.6"></a>
2162
+
2163
+ - [15.6](#comparison--nested-ternaries) Ternaries should not be nested and generally be single line expressions. eslint: [`no-nested-ternary`](https://eslint.org/docs/rules/no-nested-ternary)
2164
+
2165
+ ```javascript static
2166
+ // bad
2167
+ const foo = maybe1 > maybe2 ? "bar" : value1 > value2 ? "baz" : null;
2168
+
2169
+ // split into 2 separated ternary expressions
2170
+ const maybeNull = value1 > value2 ? "baz" : null;
2171
+
2172
+ // better
2173
+ const foo = maybe1 > maybe2 ? "bar" : maybeNull;
2174
+
2175
+ // best
2176
+ const foo = maybe1 > maybe2 ? "bar" : maybeNull;
2177
+ ```
2178
+
2179
+ <a name="comparison--unneeded-ternary"></a><a name="15.7"></a>
2180
+
2181
+ - [15.7](#comparison--unneeded-ternary) Avoid unneeded ternary statements. eslint: [`no-unneeded-ternary`](https://eslint.org/docs/rules/no-unneeded-ternary)
2182
+
2183
+ ```javascript static
2184
+ // bad
2185
+ const foo = a ? a : b;
2186
+ const bar = c ? true : false;
2187
+ const baz = c ? false : true;
2188
+ const quux = a != null ? a : b;
2189
+
2190
+ // good
2191
+ const foo = a || b;
2192
+ const bar = !!c;
2193
+ const baz = !c;
2194
+ const quux = a ?? b;
2195
+ ```
2196
+
2197
+ <a name="comparison--no-mixed-operators"></a>
2198
+
2199
+ - [15.8](#comparison--no-mixed-operators) When mixing operators, enclose them in parentheses. The only exception is the standard arithmetic operators: `+`, `-`, and `**` since their precedence is broadly understood. We recommend enclosing `/` and `*` in parentheses because their precedence can be ambiguous when they are mixed.
2200
+ eslint: [`no-mixed-operators`](https://eslint.org/docs/rules/no-mixed-operators)
2201
+
2202
+ > Why? This improves readability and clarifies the developer’s intention.
2203
+
2204
+ ```javascript static
2205
+ // bad
2206
+ const foo = (a && b < 0) || c > 0 || d + 1 === 0;
2207
+
2208
+ // bad
2209
+ const bar = a ** b - (5 % d);
2210
+
2211
+ // bad
2212
+ // one may be confused into thinking (a || b) && c
2213
+ if (a || (b && c)) {
2214
+ return d;
2215
+ }
2216
+
2217
+ // bad
2218
+ const bar = a + (b / c) * d;
2219
+
2220
+ // good
2221
+ const foo = (a && b < 0) || c > 0 || d + 1 === 0;
2222
+
2223
+ // good
2224
+ const bar = a ** b - (5 % d);
2225
+
2226
+ // good
2227
+ if (a || (b && c)) {
2228
+ return d;
2229
+ }
2230
+
2231
+ // good
2232
+ const bar = a + (b / c) * d;
2233
+ ```
2234
+
2235
+ **[⬆ back to top](#table-of-contents)**
2236
+
2237
+ ## Blocks
2238
+
2239
+ <a name="blocks--braces"></a><a name="16.1"></a>
2240
+
2241
+ - [16.1](#blocks--braces) Use braces with all multiline blocks. eslint: [`nonblock-statement-body-position`](https://eslint.org/docs/rules/nonblock-statement-body-position)
2242
+
2243
+ ```javascript static
2244
+ // bad
2245
+ if (test) return false;
2246
+
2247
+ // good
2248
+ if (test) return false;
2249
+
2250
+ // good
2251
+ if (test) {
2252
+ return false;
2253
+ }
2254
+
2255
+ // bad
2256
+ function foo() {
2257
+ return false;
2258
+ }
2259
+
2260
+ // good
2261
+ function bar() {
2262
+ return false;
2263
+ }
2264
+ ```
2265
+
2266
+ <a name="blocks--cuddled-elses"></a><a name="16.2"></a>
2267
+
2268
+ - [16.2](#blocks--cuddled-elses) If you’re using multiline blocks with `if` and `else`, put `else` on the same line as your `if` block’s closing brace. eslint: [`brace-style`](https://eslint.org/docs/rules/brace-style)
2269
+
2270
+ ```javascript static
2271
+ // bad
2272
+ if (test) {
2273
+ thing1();
2274
+ thing2();
2275
+ } else {
2276
+ thing3();
2277
+ }
2278
+
2279
+ // good
2280
+ if (test) {
2281
+ thing1();
2282
+ thing2();
2283
+ } else {
2284
+ thing3();
2285
+ }
2286
+ ```
2287
+
2288
+ <a name="blocks--no-else-return"></a><a name="16.3"></a>
2289
+
2290
+ - [16.3](#blocks--no-else-return) If an `if` block always executes a `return` statement, the subsequent `else` block is unnecessary. A `return` in an `else if` block following an `if` block that contains a `return` can be separated into multiple `if` blocks. eslint: [`no-else-return`](https://eslint.org/docs/rules/no-else-return)
2291
+
2292
+ ```javascript static
2293
+ // bad
2294
+ function foo() {
2295
+ if (x) {
2296
+ return x;
2297
+ } else {
2298
+ return y;
2299
+ }
2300
+ }
2301
+
2302
+ // bad
2303
+ function cats() {
2304
+ if (x) {
2305
+ return x;
2306
+ } else if (y) {
2307
+ return y;
2308
+ }
2309
+ }
2310
+
2311
+ // bad
2312
+ function dogs() {
2313
+ if (x) {
2314
+ return x;
2315
+ } else {
2316
+ if (y) {
2317
+ return y;
2318
+ }
2319
+ }
2320
+ }
2321
+
2322
+ // good
2323
+ function foo() {
2324
+ if (x) {
2325
+ return x;
2326
+ }
2327
+
2328
+ return y;
2329
+ }
2330
+
2331
+ // good
2332
+ function cats() {
2333
+ if (x) {
2334
+ return x;
2335
+ }
2336
+
2337
+ if (y) {
2338
+ return y;
2339
+ }
2340
+ }
2341
+
2342
+ // good
2343
+ function dogs(x) {
2344
+ if (x) {
2345
+ if (z) {
2346
+ return y;
2347
+ }
2348
+ } else {
2349
+ return z;
2350
+ }
2351
+ }
2352
+ ```
2353
+
2354
+ **[⬆ back to top](#table-of-contents)**
2355
+
2356
+ ## Control Statements
2357
+
2358
+ <a name="control-statements"></a>
2359
+
2360
+ - [17.1](#control-statements) In case your control statement (`if`, `while` etc.) gets too long or exceeds the maximum line length, each (grouped) condition could be put into a new line. The logical operator should begin the line.
2361
+
2362
+ > Why? Requiring operators at the beginning of the line keeps the operators aligned and follows a pattern similar to method chaining. This also improves readability by making it easier to visually follow complex logic.
2363
+
2364
+ ```javascript static
2365
+ // bad
2366
+ if (
2367
+ (foo === 123 || bar === "abc") &&
2368
+ doesItLookGoodWhenItBecomesThatLong() &&
2369
+ isThisReallyHappening()
2370
+ ) {
2371
+ thing1();
2372
+ }
2373
+
2374
+ // bad
2375
+ if (foo === 123 && bar === "abc") {
2376
+ thing1();
2377
+ }
2378
+
2379
+ // bad
2380
+ if (foo === 123 && bar === "abc") {
2381
+ thing1();
2382
+ }
2383
+
2384
+ // bad
2385
+ if (foo === 123 && bar === "abc") {
2386
+ thing1();
2387
+ }
2388
+
2389
+ // good
2390
+ if (foo === 123 && bar === "abc") {
2391
+ thing1();
2392
+ }
2393
+
2394
+ // good
2395
+ if (
2396
+ (foo === 123 || bar === "abc") &&
2397
+ doesItLookGoodWhenItBecomesThatLong() &&
2398
+ isThisReallyHappening()
2399
+ ) {
2400
+ thing1();
2401
+ }
2402
+
2403
+ // good
2404
+ if (foo === 123 && bar === "abc") {
2405
+ thing1();
2406
+ }
2407
+ ```
2408
+
2409
+ <a name="control-statement--value-selection"></a><a name="control-statements--value-selection"></a>
2410
+
2411
+ - [17.2](#control-statements--value-selection) Don't use selection operators in place of control statements.
2412
+
2413
+ ```javascript static
2414
+ // bad
2415
+ !isRunning && startRunning();
2416
+
2417
+ // good
2418
+ if (!isRunning) {
2419
+ startRunning();
2420
+ }
2421
+ ```
2422
+
2423
+ **[⬆ back to top](#table-of-contents)**
2424
+
2425
+ ## Comments
2426
+
2427
+ <a name="comments--multiline"></a><a name="17.1"></a>
2428
+
2429
+ - [18.1](#comments--multiline) Use `/** ... */` for multiline comments.
2430
+
2431
+ ```javascript static
2432
+ // bad
2433
+ // make() returns a new element
2434
+ // based on the passed in tag name
2435
+ //
2436
+ // @param {String} tag
2437
+ // @return {Element} element
2438
+ function make(tag) {
2439
+ // ...
2440
+
2441
+ return element;
2442
+ }
2443
+
2444
+ // good
2445
+ /**
2446
+ * make() returns a new element
2447
+ * based on the passed-in tag name
2448
+ */
2449
+ function make(tag) {
2450
+ // ...
2451
+
2452
+ return element;
2453
+ }
2454
+ ```
2455
+
2456
+ <a name="comments--singleline"></a><a name="17.2"></a>
2457
+
2458
+ - [18.2](#comments--singleline) Use `//` for single line comments. Place single line comments on a newline above the subject of the comment. Put an empty line before the comment unless it’s on the first line of a block.
2459
+
2460
+ ```javascript static
2461
+ // bad
2462
+ const active = true; // is current tab
2463
+
2464
+ // good
2465
+ // is current tab
2466
+ const active = true;
2467
+
2468
+ // bad
2469
+ function getType() {
2470
+ console.log("fetching type...");
2471
+ // set the default type to 'no type'
2472
+ const type = this.type || "no type";
2473
+
2474
+ return type;
2475
+ }
2476
+
2477
+ // good
2478
+ function getType() {
2479
+ console.log("fetching type...");
2480
+
2481
+ // set the default type to 'no type'
2482
+ const type = this.type || "no type";
2483
+
2484
+ return type;
2485
+ }
2486
+
2487
+ // also good
2488
+ function getType() {
2489
+ // set the default type to 'no type'
2490
+ const type = this.type || "no type";
2491
+
2492
+ return type;
2493
+ }
2494
+ ```
2495
+
2496
+ <a name="comments--spaces"></a>
2497
+
2498
+ - [18.3](#comments--spaces) Start all comments with a space to make it easier to read. eslint: [`spaced-comment`](https://eslint.org/docs/rules/spaced-comment)
2499
+
2500
+ ```javascript static
2501
+ // bad
2502
+ //is current tab
2503
+ const active = true;
2504
+
2505
+ // good
2506
+ // is current tab
2507
+ const active = true;
2508
+
2509
+ // bad
2510
+ /**
2511
+ *make() returns a new element
2512
+ *based on the passed-in tag name
2513
+ */
2514
+ function make(tag) {
2515
+ // ...
2516
+
2517
+ return element;
2518
+ }
2519
+
2520
+ // good
2521
+ /**
2522
+ * make() returns a new element
2523
+ * based on the passed-in tag name
2524
+ */
2525
+ function make(tag) {
2526
+ // ...
2527
+
2528
+ return element;
2529
+ }
2530
+ ```
2531
+
2532
+ <a name="comments--actionitems"></a><a name="17.3"></a>
2533
+
2534
+ - [18.4](#comments--actionitems) Prefixing your comments with `FIXME` or `TODO` helps other developers quickly understand if you’re pointing out a problem that needs to be revisited, or if you’re suggesting a solution to the problem that needs to be implemented. These are different than regular comments because they are actionable. The actions are `FIXME: -- need to figure this out` or `TODO: -- need to implement`.
2535
+
2536
+ <a name="comments--fixme"></a><a name="17.4"></a>
2537
+
2538
+ - [18.5](#comments--fixme) Use `// FIXME:` to annotate problems.
2539
+
2540
+ ```javascript static
2541
+ class Calculator extends Abacus {
2542
+ constructor() {
2543
+ super();
2544
+
2545
+ // FIXME: shouldn’t use a global here
2546
+ total = 0;
2547
+ }
2548
+ }
2549
+ ```
2550
+
2551
+ <a name="comments--todo"></a><a name="17.5"></a>
2552
+
2553
+ - [18.6](#comments--todo) Use `// TODO:` to annotate solutions to problems.
2554
+
2555
+ ```javascript static
2556
+ class Calculator extends Abacus {
2557
+ constructor() {
2558
+ super();
2559
+
2560
+ // TODO: total should be configurable by an options param
2561
+ this.total = 0;
2562
+ }
2563
+ }
2564
+ ```
2565
+
2566
+ **[⬆ back to top](#table-of-contents)**
2567
+
2568
+ ## Whitespace
2569
+
2570
+ <a name="whitespace--spaces"></a><a name="18.1"></a>
2571
+
2572
+ - [19.1](#whitespace--spaces) Use soft tabs (space character) set to 2 spaces. eslint: [`indent`](https://eslint.org/docs/rules/indent)
2573
+
2574
+ ```javascript static
2575
+ // bad
2576
+ function foo() {
2577
+ ∙∙∙∙let name;
2578
+ }
2579
+
2580
+ // bad
2581
+ function bar() {
2582
+ ∙let name;
2583
+ }
2584
+
2585
+ // good
2586
+ function baz() {
2587
+ ∙∙let name;
2588
+ }
2589
+ ```
2590
+
2591
+ <a name="whitespace--before-blocks"></a><a name="18.2"></a>
2592
+
2593
+ - [19.2](#whitespace--before-blocks) Place 1 space before the leading brace. eslint: [`space-before-blocks`](https://eslint.org/docs/rules/space-before-blocks)
2594
+
2595
+ ```javascript static
2596
+ // bad
2597
+ function test() {
2598
+ console.log("test");
2599
+ }
2600
+
2601
+ // good
2602
+ function test() {
2603
+ console.log("test");
2604
+ }
2605
+
2606
+ // bad
2607
+ dog.set("attr", {
2608
+ age: "1 year",
2609
+ breed: "Bernese Mountain Dog",
2610
+ });
2611
+
2612
+ // good
2613
+ dog.set("attr", {
2614
+ age: "1 year",
2615
+ breed: "Bernese Mountain Dog",
2616
+ });
2617
+ ```
2618
+
2619
+ <a name="whitespace--around-keywords"></a><a name="18.3"></a>
2620
+
2621
+ - [19.3](#whitespace--around-keywords) Place 1 space before the opening parenthesis in control statements (`if`, `while` etc.). Place no space between the argument list and the function name in function calls and declarations. eslint: [`keyword-spacing`](https://eslint.org/docs/rules/keyword-spacing)
2622
+
2623
+ ```javascript static
2624
+ // bad
2625
+ if (isJedi) {
2626
+ fight();
2627
+ }
2628
+
2629
+ // good
2630
+ if (isJedi) {
2631
+ fight();
2632
+ }
2633
+
2634
+ // bad
2635
+ function fight() {
2636
+ console.log("Swooosh!");
2637
+ }
2638
+
2639
+ // good
2640
+ function fight() {
2641
+ console.log("Swooosh!");
2642
+ }
2643
+ ```
2644
+
2645
+ <a name="whitespace--infix-ops"></a><a name="18.4"></a>
2646
+
2647
+ - [19.4](#whitespace--infix-ops) Set off operators with spaces. eslint: [`space-infix-ops`](https://eslint.org/docs/rules/space-infix-ops)
2648
+
2649
+ ```javascript static
2650
+ // bad
2651
+ const x = y + 5;
2652
+
2653
+ // good
2654
+ const x = y + 5;
2655
+ ```
2656
+
2657
+ <a name="whitespace--newline-at-end"></a><a name="18.5"></a>
2658
+
2659
+ - [19.5](#whitespace--newline-at-end) End files with a single newline character. eslint: [`eol-last`](https://eslint.org/docs/rules/eol-last)
2660
+
2661
+ ```javascript static
2662
+ // bad
2663
+ import { es6 } from "./AirbnbStyleGuide";
2664
+ // ...
2665
+ export default es6;
2666
+ ```
2667
+
2668
+ ```javascript static
2669
+ // bad
2670
+ import { es6 } from './AirbnbStyleGuide';
2671
+ // ...
2672
+ export default es6;↵
2673
+
2674
+ ```
2675
+
2676
+ ```javascript static
2677
+ // good
2678
+ import { es6 } from './AirbnbStyleGuide';
2679
+ // ...
2680
+ export default es6;↵
2681
+ ```
2682
+
2683
+ <a name="whitespace--chains"></a><a name="18.6"></a>
2684
+
2685
+ - [19.6](#whitespace--chains) Use indentation when making long method chains (more than 2 method chains). Use a leading dot, which
2686
+ emphasizes that the line is a method call, not a new statement. eslint: [`newline-per-chained-call`](https://eslint.org/docs/rules/newline-per-chained-call) [`no-whitespace-before-property`](https://eslint.org/docs/rules/no-whitespace-before-property)
2687
+
2688
+ ```javascript static
2689
+ // bad
2690
+ $("#items").find(".selected").highlight().end().find(".open").updateCount();
2691
+
2692
+ // bad
2693
+ $("#items").find(".selected").highlight().end().find(".open").updateCount();
2694
+
2695
+ // good
2696
+ $("#items").find(".selected").highlight().end().find(".open").updateCount();
2697
+
2698
+ // bad
2699
+ const leds = stage
2700
+ .selectAll(".led")
2701
+ .data(data)
2702
+ .enter()
2703
+ .append("svg:svg")
2704
+ .classed("led", true)
2705
+ .attr("width", (radius + margin) * 2)
2706
+ .append("svg:g")
2707
+ .attr("transform", `translate(${radius + margin}, ${radius + margin})`)
2708
+ .call(tron.led);
2709
+
2710
+ // good
2711
+ const leds = stage
2712
+ .selectAll(".led")
2713
+ .data(data)
2714
+ .enter()
2715
+ .append("svg:svg")
2716
+ .classed("led", true)
2717
+ .attr("width", (radius + margin) * 2)
2718
+ .append("svg:g")
2719
+ .attr("transform", `translate(${radius + margin}, ${radius + margin})`)
2720
+ .call(tron.led);
2721
+
2722
+ // good
2723
+ const leds = stage.selectAll(".led").data(data);
2724
+ const svg = leds.enter().append("svg:svg");
2725
+ svg.classed("led", true).attr("width", (radius + margin) * 2);
2726
+ const g = svg.append("svg:g");
2727
+ g.attr("transform", `translate(${radius + margin}, ${radius + margin})`).call(
2728
+ tron.led
2729
+ );
2730
+ ```
2731
+
2732
+ <a name="whitespace--after-blocks"></a><a name="18.7"></a>
2733
+
2734
+ - [19.7](#whitespace--after-blocks) Leave a blank line after blocks and before the next statement.
2735
+
2736
+ ```javascript static
2737
+ // bad
2738
+ if (foo) {
2739
+ return bar;
2740
+ }
2741
+ return baz;
2742
+
2743
+ // good
2744
+ if (foo) {
2745
+ return bar;
2746
+ }
2747
+
2748
+ return baz;
2749
+
2750
+ // bad
2751
+ const obj = {
2752
+ foo() {},
2753
+ bar() {},
2754
+ };
2755
+ return obj;
2756
+
2757
+ // good
2758
+ const obj = {
2759
+ foo() {},
2760
+
2761
+ bar() {},
2762
+ };
2763
+
2764
+ return obj;
2765
+
2766
+ // bad
2767
+ const arr = [function foo() {}, function bar() {}];
2768
+ return arr;
2769
+
2770
+ // good
2771
+ const arr = [function foo() {}, function bar() {}];
2772
+
2773
+ return arr;
2774
+ ```
2775
+
2776
+ <a name="whitespace--padded-blocks"></a><a name="18.8"></a>
2777
+
2778
+ - [19.8](#whitespace--padded-blocks) Do not pad your blocks with blank lines. eslint: [`padded-blocks`](https://eslint.org/docs/rules/padded-blocks)
2779
+
2780
+ ```javascript static
2781
+ // bad
2782
+ function bar() {
2783
+ console.log(foo);
2784
+ }
2785
+
2786
+ // bad
2787
+ if (baz) {
2788
+ console.log(quux);
2789
+ } else {
2790
+ console.log(foo);
2791
+ }
2792
+
2793
+ // bad
2794
+ class Foo {
2795
+ constructor(bar) {
2796
+ this.bar = bar;
2797
+ }
2798
+ }
2799
+
2800
+ // good
2801
+ function bar() {
2802
+ console.log(foo);
2803
+ }
2804
+
2805
+ // good
2806
+ if (baz) {
2807
+ console.log(quux);
2808
+ } else {
2809
+ console.log(foo);
2810
+ }
2811
+ ```
2812
+
2813
+
2814
+
2815
+ <a name="whitespace--no-multiple-blanks"></a>
2816
+ - [19.9](#whitespace--no-multiple-blanks) Do not use multiple blank lines to pad your code. eslint: [`no-multiple-empty-lines`](https://eslint.org/docs/rules/no-multiple-empty-lines)
2817
+
2818
+ ```javascript static
2819
+
2820
+ // bad
2821
+ class Person {
2822
+ constructor(fullName, email, birthday) {
2823
+ this.fullName = fullName;
2824
+
2825
+
2826
+ this.email = email;
2827
+
2828
+ this.setAge(birthday);
2829
+ }
2830
+
2831
+ setAge(birthday) {
2832
+ const today = new Date();
2833
+
2834
+
2835
+ const age = this.getAge(today, birthday);
2836
+
2837
+ this.age = age;
2838
+ }
2839
+
2840
+ getAge(today, birthday) {
2841
+ // ..
2842
+ }
2843
+ }
2844
+
2845
+ // good
2846
+ class Person {
2847
+ constructor(fullName, email, birthday) {
2848
+ this.fullName = fullName;
2849
+ this.email = email;
2850
+ this.setAge(birthday);
2851
+ }
2852
+
2853
+ setAge(birthday) {
2854
+ const today = new Date();
2855
+ const age = getAge(today, birthday);
2856
+ this.age = age;
2857
+ }
2858
+
2859
+ getAge(today, birthday) {
2860
+ // ..
2861
+ }
2862
+ }
2863
+
2864
+ ```
2865
+
2866
+ <a name="whitespace--in-parens"></a><a name="18.9"></a>
2867
+
2868
+ - [19.10](#whitespace--in-parens) Do not add spaces inside parentheses. eslint: [`space-in-parens`](https://eslint.org/docs/rules/space-in-parens)
2869
+
2870
+ ```javascript static
2871
+ // bad
2872
+ function bar(foo) {
2873
+ return foo;
2874
+ }
2875
+
2876
+ // good
2877
+ function bar(foo) {
2878
+ return foo;
2879
+ }
2880
+
2881
+ // bad
2882
+ if (foo) {
2883
+ console.log(foo);
2884
+ }
2885
+
2886
+ // good
2887
+ if (foo) {
2888
+ console.log(foo);
2889
+ }
2890
+ ```
2891
+
2892
+ <a name="whitespace--in-brackets"></a><a name="18.10"></a>
2893
+
2894
+ - [19.11](#whitespace--in-brackets) Do not add spaces inside brackets. eslint: [`array-bracket-spacing`](https://eslint.org/docs/rules/array-bracket-spacing)
2895
+
2896
+ ```javascript static
2897
+ // bad
2898
+ const foo = [1, 2, 3];
2899
+ console.log(foo[0]);
2900
+
2901
+ // good
2902
+ const foo = [1, 2, 3];
2903
+ console.log(foo[0]);
2904
+ ```
2905
+
2906
+ <a name="whitespace--in-braces"></a><a name="18.11"></a>
2907
+
2908
+ - [19.12](#whitespace--in-braces) Add spaces inside curly braces. eslint: [`object-curly-spacing`](https://eslint.org/docs/rules/object-curly-spacing)
2909
+
2910
+ ```javascript static
2911
+ // bad
2912
+ const foo = { clark: "kent" };
2913
+
2914
+ // good
2915
+ const foo = { clark: "kent" };
2916
+ ```
2917
+
2918
+ <a name="whitespace--max-len"></a><a name="18.12"></a>
2919
+
2920
+ - [19.13](#whitespace--max-len) Avoid having lines of code that are longer than 100 characters (including whitespace). Note: per [above](#strings--line-length), long strings are exempt from this rule, and should not be broken up. eslint: [`max-len`](https://eslint.org/docs/rules/max-len)
2921
+
2922
+ > Why? This ensures readability and maintainability.
2923
+
2924
+ ```javascript static
2925
+ // bad
2926
+ const foo =
2927
+ jsonData &&
2928
+ jsonData.foo &&
2929
+ jsonData.foo.bar &&
2930
+ jsonData.foo.bar.baz &&
2931
+ jsonData.foo.bar.baz.quux &&
2932
+ jsonData.foo.bar.baz.quux.xyzzy;
2933
+
2934
+ // bad
2935
+ $.ajax({ method: "POST", url: "https://airbnb.com/", data: { name: "John" } })
2936
+ .done(() => console.log("Congratulations!"))
2937
+ .fail(() => console.log("You have failed this city."));
2938
+
2939
+ // good
2940
+ const foo =
2941
+ jsonData &&
2942
+ jsonData.foo &&
2943
+ jsonData.foo.bar &&
2944
+ jsonData.foo.bar.baz &&
2945
+ jsonData.foo.bar.baz.quux &&
2946
+ jsonData.foo.bar.baz.quux.xyzzy;
2947
+
2948
+ // better
2949
+ const foo = jsonData?.foo?.bar?.baz?.quux?.xyzzy;
2950
+
2951
+ // good
2952
+ $.ajax({
2953
+ method: "POST",
2954
+ url: "https://airbnb.com/",
2955
+ data: { name: "John" },
2956
+ })
2957
+ .done(() => console.log("Congratulations!"))
2958
+ .fail(() => console.log("You have failed this city."));
2959
+ ```
2960
+
2961
+ <a name="whitespace--block-spacing"></a>
2962
+
2963
+ - [19.14](#whitespace--block-spacing) Require consistent spacing inside an open block token and the next token on the same line. This rule also enforces consistent spacing inside a close block token and previous token on the same line. eslint: [`block-spacing`](https://eslint.org/docs/rules/block-spacing)
2964
+
2965
+ ```javascript static
2966
+ // bad
2967
+ function foo() {
2968
+ return true;
2969
+ }
2970
+ if (foo) {
2971
+ bar = 0;
2972
+ }
2973
+
2974
+ // good
2975
+ function foo() {
2976
+ return true;
2977
+ }
2978
+ if (foo) {
2979
+ bar = 0;
2980
+ }
2981
+ ```
2982
+
2983
+ <a name="whitespace--comma-spacing"></a>
2984
+
2985
+ - [19.15](#whitespace--comma-spacing) Avoid spaces before commas and require a space after commas. eslint: [`comma-spacing`](https://eslint.org/docs/rules/comma-spacing)
2986
+
2987
+ ```javascript static
2988
+ // bad
2989
+ const foo = 1,
2990
+ bar = 2;
2991
+ const arr = [1, 2];
2992
+
2993
+ // good
2994
+ const foo = 1,
2995
+ bar = 2;
2996
+ const arr = [1, 2];
2997
+ ```
2998
+
2999
+ <a name="whitespace--computed-property-spacing"></a>
3000
+
3001
+ - [19.16](#whitespace--computed-property-spacing) Enforce spacing inside of computed property brackets. eslint: [`computed-property-spacing`](https://eslint.org/docs/rules/computed-property-spacing)
3002
+
3003
+ ```javascript static
3004
+ // bad
3005
+ obj[foo];
3006
+ obj["foo"];
3007
+ const x = { [b]: a };
3008
+ obj[foo[bar]];
3009
+
3010
+ // good
3011
+ obj[foo];
3012
+ obj["foo"];
3013
+ const x = { [b]: a };
3014
+ obj[foo[bar]];
3015
+ ```
3016
+
3017
+ <a name="whitespace--func-call-spacing"></a>
3018
+
3019
+ - [19.17](#whitespace--func-call-spacing) Avoid spaces between functions and their invocations. eslint: [`func-call-spacing`](https://eslint.org/docs/rules/func-call-spacing)
3020
+
3021
+ ```javascript static
3022
+ // bad
3023
+ func();
3024
+
3025
+ func();
3026
+
3027
+ // good
3028
+ func();
3029
+ ```
3030
+
3031
+ <a name="whitespace--key-spacing"></a>
3032
+
3033
+ - [19.18](#whitespace--key-spacing) Enforce spacing between keys and values in object literal properties. eslint: [`key-spacing`](https://eslint.org/docs/rules/key-spacing)
3034
+
3035
+ ```javascript static
3036
+ // bad
3037
+ const obj = { foo: 42 };
3038
+ const obj2 = { foo: 42 };
3039
+
3040
+ // good
3041
+ const obj = { foo: 42 };
3042
+ ```
3043
+
3044
+ <a name="whitespace--no-trailing-spaces"></a>
3045
+
3046
+ - [19.19](#whitespace--no-trailing-spaces) Avoid trailing spaces at the end of lines. eslint: [`no-trailing-spaces`](https://eslint.org/docs/rules/no-trailing-spaces)
3047
+
3048
+ <a name="whitespace--no-multiple-empty-lines"></a>
3049
+
3050
+ - [19.20](#whitespace--no-multiple-empty-lines) Avoid multiple empty lines, only allow one newline at the end of files, and avoid a newline at the beginning of files. eslint: [`no-multiple-empty-lines`](https://eslint.org/docs/rules/no-multiple-empty-lines)
3051
+
3052
+ <!-- markdownlint-disable MD012 -->
3053
+
3054
+ ```javascript static
3055
+ // bad - multiple empty lines
3056
+ const x = 1;
3057
+
3058
+ const y = 2;
3059
+
3060
+ // bad - 2+ newlines at end of file
3061
+ const x = 1;
3062
+ const y = 2;
3063
+
3064
+ // bad - 1+ newline(s) at beginning of file
3065
+
3066
+ const x = 1;
3067
+ const y = 2;
3068
+
3069
+ // good
3070
+ const x = 1;
3071
+ const y = 2;
3072
+ ```
3073
+
3074
+ <!-- markdownlint-enable MD012 -->
3075
+
3076
+ **[⬆ back to top](#table-of-contents)**
3077
+
3078
+ ## Commas
3079
+
3080
+ <a name="commas--leading-trailing"></a><a name="19.1"></a>
3081
+
3082
+ - [20.1](#commas--leading-trailing) Leading commas: **Nope.** eslint: [`comma-style`](https://eslint.org/docs/rules/comma-style)
3083
+
3084
+ ```javascript static
3085
+ // bad
3086
+ const story = [once, upon, aTime];
3087
+
3088
+ // good
3089
+ const story = [once, upon, aTime];
3090
+
3091
+ // bad
3092
+ const hero = {
3093
+ firstName: "Ada",
3094
+ lastName: "Lovelace",
3095
+ birthYear: 1815,
3096
+ superPower: "computers",
3097
+ };
3098
+
3099
+ // good
3100
+ const hero = {
3101
+ firstName: "Ada",
3102
+ lastName: "Lovelace",
3103
+ birthYear: 1815,
3104
+ superPower: "computers",
3105
+ };
3106
+ ```
3107
+
3108
+ <a name="commas--dangling"></a><a name="19.2"></a>
3109
+
3110
+ - [20.2](#commas--dangling) Additional trailing comma: **Yup.** eslint: [`comma-dangle`](https://eslint.org/docs/rules/comma-dangle)
3111
+
3112
+ > Why? This leads to cleaner git diffs. Also, transpilers like Babel will remove the additional trailing comma in the transpiled code which means you don’t have to worry about the [trailing comma problem](https://github.com/airbnb/javascript/blob/es5-deprecated/es5/README.md#commas) in legacy browsers.
3113
+
3114
+ ```diff
3115
+ // bad - git diff without trailing comma
3116
+ const hero = {
3117
+ firstName: 'Florence',
3118
+ - lastName: 'Nightingale'
3119
+ + lastName: 'Nightingale',
3120
+ + inventorOf: ['coxcomb chart', 'modern nursing']
3121
+ };
3122
+
3123
+ // good - git diff with trailing comma
3124
+ const hero = {
3125
+ firstName: 'Florence',
3126
+ lastName: 'Nightingale',
3127
+ + inventorOf: ['coxcomb chart', 'modern nursing'],
3128
+ };
3129
+ ```
3130
+
3131
+ ```javascript static
3132
+ // bad
3133
+ const hero = {
3134
+ firstName: "Dana",
3135
+ lastName: "Scully",
3136
+ };
3137
+
3138
+ const heroes = ["Batman", "Superman"];
3139
+
3140
+ // good
3141
+ const hero = {
3142
+ firstName: "Dana",
3143
+ lastName: "Scully",
3144
+ };
3145
+
3146
+ const heroes = ["Batman", "Superman"];
3147
+
3148
+ // bad
3149
+ function createHero(firstName, lastName, inventorOf) {
3150
+ // does nothing
3151
+ }
3152
+
3153
+ // good
3154
+ function createHero(firstName, lastName, inventorOf) {
3155
+ // does nothing
3156
+ }
3157
+
3158
+ // good (note that a comma must not appear after a "rest" element)
3159
+ function createHero(firstName, lastName, inventorOf, ...heroArgs) {
3160
+ // does nothing
3161
+ }
3162
+
3163
+ // bad
3164
+ createHero(firstName, lastName, inventorOf);
3165
+
3166
+ // good
3167
+ createHero(firstName, lastName, inventorOf);
3168
+
3169
+ // good (note that a comma must not appear after a "rest" element)
3170
+ createHero(firstName, lastName, inventorOf, ...heroArgs);
3171
+ ```
3172
+
3173
+ **[⬆ back to top](#table-of-contents)**
3174
+
3175
+ ## Semicolons
3176
+
3177
+ <a name="semicolons--required"></a><a name="20.1"></a>
3178
+
3179
+ - [21.1](#semicolons--required) **Yup.** eslint: [`semi`](https://eslint.org/docs/rules/semi)
3180
+
3181
+ > Why? When JavaScript encounters a line break without a semicolon, it uses a set of rules called [Automatic Semicolon Insertion](https://tc39.github.io/ecma262/#sec-automatic-semicolon-insertion) to determine whether it should regard that line break as the end of a statement, and (as the name implies) place a semicolon into your code before the line break if it thinks so. ASI contains a few eccentric behaviors, though, and your code will break if JavaScript misinterprets your line break. These rules will become more complicated as new features become a part of JavaScript. Explicitly terminating your statements and configuring your linter to catch missing semicolons will help prevent you from encountering issues.
3182
+
3183
+ ```javascript static
3184
+ // bad - raises exception
3185
+ const luke = {};
3186
+ const leia = {}[(luke, leia)].forEach((jedi) => (jedi.father = "vader"));
3187
+
3188
+ // bad - raises exception
3189
+ const reaction = "No! That’s impossible!"(
3190
+ (async function meanwhileOnTheFalcon() {
3191
+ // handle `leia`, `lando`, `chewie`, `r2`, `c3p0`
3192
+ // ...
3193
+ })()
3194
+ );
3195
+
3196
+ // bad - returns `undefined` instead of the value on the next line - always happens when `return` is on a line by itself because of ASI!
3197
+ function foo() {
3198
+ return;
3199
+ ("search your feelings, you know it to be foo");
3200
+ }
3201
+
3202
+ // good
3203
+ const luke = {};
3204
+ const leia = {};
3205
+ [luke, leia].forEach((jedi) => {
3206
+ jedi.father = "vader";
3207
+ });
3208
+
3209
+ // good
3210
+ const reaction = "No! That’s impossible!";
3211
+ (async function meanwhileOnTheFalcon() {
3212
+ // handle `leia`, `lando`, `chewie`, `r2`, `c3p0`
3213
+ // ...
3214
+ })();
3215
+
3216
+ // good
3217
+ function foo() {
3218
+ return "search your feelings, you know it to be foo";
3219
+ }
3220
+ ```
3221
+
3222
+ [Read more](https://stackoverflow.com/questions/7365172/semicolon-before-self-invoking-function/7365214#7365214).
3223
+
3224
+ **[⬆ back to top](#table-of-contents)**
3225
+
3226
+ ## Type Casting & Coercion
3227
+
3228
+ <a name="coercion--explicit"></a><a name="21.1"></a>
3229
+
3230
+ - [22.1](#coercion--explicit) Perform type coercion at the beginning of the statement.
3231
+
3232
+ <a name="coercion--strings"></a><a name="21.2"></a>
3233
+
3234
+ - [22.2](#coercion--strings) Strings: eslint: [`no-new-wrappers`](https://eslint.org/docs/rules/no-new-wrappers)
3235
+
3236
+ ```javascript static
3237
+ // => this.reviewScore = 9;
3238
+
3239
+ // bad
3240
+ const totalScore = new String(this.reviewScore); // typeof totalScore is "object" not "string"
3241
+
3242
+ // bad
3243
+ const totalScore = this.reviewScore + ""; // invokes this.reviewScore.valueOf()
3244
+
3245
+ // bad
3246
+ const totalScore = this.reviewScore.toString(); // isn’t guaranteed to return a string
3247
+
3248
+ // good
3249
+ const totalScore = String(this.reviewScore);
3250
+ ```
3251
+
3252
+ <a name="coercion--numbers"></a><a name="21.3"></a>
3253
+
3254
+ - [22.3](#coercion--numbers) Numbers: Use `Number` for type casting and `parseInt` always with a radix for parsing strings. eslint: [`radix`](https://eslint.org/docs/rules/radix) [`no-new-wrappers`](https://eslint.org/docs/rules/no-new-wrappers)
3255
+
3256
+ > Why? The `parseInt` function produces an integer value dictated by interpretation of the contents of the string argument according to the specified radix. Leading whitespace in string is ignored. If radix is `undefined` or `0`, it is assumed to be `10` except when the number begins with the character pairs `0x` or `0X`, in which case a radix of 16 is assumed. This differs from ECMAScript 3, which merely discouraged (but allowed) octal interpretation. Many implementations have not adopted this behavior as of 2013. And, because older browsers must be supported, always specify a radix.
3257
+
3258
+ ```javascript static
3259
+ const inputValue = "4";
3260
+
3261
+ // bad
3262
+ const val = new Number(inputValue);
3263
+
3264
+ // bad
3265
+ const val = +inputValue;
3266
+
3267
+ // bad
3268
+ const val = inputValue >> 0;
3269
+
3270
+ // bad
3271
+ const val = parseInt(inputValue);
3272
+
3273
+ // good
3274
+ const val = Number(inputValue);
3275
+
3276
+ // good
3277
+ const val = parseInt(inputValue, 10);
3278
+ ```
3279
+
3280
+ <a name="coercion--comment-deviations"></a><a name="21.4"></a>
3281
+
3282
+ - [22.4](#coercion--comment-deviations) If for whatever reason you are doing something wild and `parseInt` is your bottleneck and need to use Bitshift for [performance reasons](https://web.archive.org/web/20200414205431/https://jsperf.com/coercion-vs-casting/3), leave a comment explaining why and what you’re doing.
3283
+
3284
+ ```javascript static
3285
+ // good
3286
+ /**
3287
+ * parseInt was the reason my code was slow.
3288
+ * Bitshifting the String to coerce it to a
3289
+ * Number made it a lot faster.
3290
+ */
3291
+ const val = inputValue >> 0;
3292
+ ```
3293
+
3294
+ <a name="coercion--bitwise"></a><a name="21.5"></a>
3295
+
3296
+ - [22.5](#coercion--bitwise) **Note:** Be careful when using bitshift operations. Numbers are represented as [64-bit values](https://es5.github.io/#x4.3.19), but bitshift operations always return a 32-bit integer ([source](https://es5.github.io/#x11.7)). Bitshift can lead to unexpected behavior for integer values larger than 32 bits. [Discussion](https://github.com/airbnb/javascript/issues/109). Largest signed 32-bit Int is 2,147,483,647:
3297
+
3298
+ ```javascript static
3299
+ 2147483647 >> 0; // => 2147483647
3300
+ 2147483648 >> 0; // => -2147483648
3301
+ 2147483649 >> 0; // => -2147483647
3302
+ ```
3303
+
3304
+ <a name="coercion--booleans"></a><a name="21.6"></a>
3305
+
3306
+ - [22.6](#coercion--booleans) Booleans: eslint: [`no-new-wrappers`](https://eslint.org/docs/rules/no-new-wrappers)
3307
+
3308
+ ```javascript static
3309
+ const age = 0;
3310
+
3311
+ // bad
3312
+ const hasAge = new Boolean(age);
3313
+
3314
+ // good
3315
+ const hasAge = Boolean(age);
3316
+
3317
+ // best
3318
+ const hasAge = !!age;
3319
+ ```
3320
+
3321
+ **[⬆ back to top](#table-of-contents)**
3322
+
3323
+ ## Naming Conventions
3324
+
3325
+ <a name="naming--descriptive"></a><a name="22.1"></a>
3326
+
3327
+ - [23.1](#naming--descriptive) Avoid single letter names. Be descriptive with your naming. eslint: [`id-length`](https://eslint.org/docs/rules/id-length)
3328
+
3329
+ ```javascript static
3330
+ // bad
3331
+ function q() {
3332
+ // ...
3333
+ }
3334
+
3335
+ // good
3336
+ function query() {
3337
+ // ...
3338
+ }
3339
+ ```
3340
+
3341
+ <a name="naming--camelCase"></a><a name="22.2"></a>
3342
+
3343
+ - [23.2](#naming--camelCase) Use camelCase when naming objects, functions, and instances. eslint: [`camelcase`](https://eslint.org/docs/rules/camelcase)
3344
+
3345
+ ```javascript static
3346
+ // bad
3347
+ const OBJEcttsssss = {};
3348
+ const this_is_my_object = {};
3349
+ function c() {}
3350
+
3351
+ // good
3352
+ const thisIsMyObject = {};
3353
+ function thisIsMyFunction() {}
3354
+ ```
3355
+
3356
+ <a name="naming--PascalCase"></a><a name="22.3"></a>
3357
+
3358
+ - [23.3](#naming--PascalCase) Use PascalCase only when naming constructors or classes. eslint: [`new-cap`](https://eslint.org/docs/rules/new-cap)
3359
+
3360
+ ```javascript static
3361
+ // bad
3362
+ function user(options) {
3363
+ this.name = options.name;
3364
+ }
3365
+
3366
+ const bad = new user({
3367
+ name: "nope",
3368
+ });
3369
+
3370
+ // good
3371
+ class User {
3372
+ constructor(options) {
3373
+ this.name = options.name;
3374
+ }
3375
+ }
3376
+
3377
+ const good = new User({
3378
+ name: "yup",
3379
+ });
3380
+ ```
3381
+
3382
+ <a name="naming--leading-underscore"></a><a name="22.4"></a>
3383
+
3384
+ - [23.4](#naming--leading-underscore) Do not use trailing or leading underscores. eslint: [`no-underscore-dangle`](https://eslint.org/docs/rules/no-underscore-dangle)
3385
+
3386
+ > Why? JavaScript does not have the concept of privacy in terms of properties or methods. Although a leading underscore is a common convention to mean “private”, in fact, these properties are fully public, and as such, are part of your public API contract. This convention might lead developers to wrongly think that a change won’t count as breaking, or that tests aren’t needed. tl;dr: if you want something to be “private”, it must not be observably present.
3387
+
3388
+ ```javascript static
3389
+ // bad
3390
+ this.__firstName__ = "Panda";
3391
+ this.firstName_ = "Panda";
3392
+ this._firstName = "Panda";
3393
+
3394
+ // good
3395
+ this.firstName = "Panda";
3396
+
3397
+ // good, in environments where WeakMaps are available
3398
+ // see https://kangax.github.io/compat-table/es6/#test-WeakMap
3399
+ const firstNames = new WeakMap();
3400
+ firstNames.set(this, "Panda");
3401
+ ```
3402
+
3403
+ <a name="naming--self-this"></a><a name="22.5"></a>
3404
+
3405
+ - [23.5](#naming--self-this) Don’t save references to `this`. Use arrow functions or [Function#bind](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind).
3406
+
3407
+ ```javascript static
3408
+ // bad
3409
+ function foo() {
3410
+ const self = this;
3411
+ return function () {
3412
+ console.log(self);
3413
+ };
3414
+ }
3415
+
3416
+ // bad
3417
+ function foo() {
3418
+ const that = this;
3419
+ return function () {
3420
+ console.log(that);
3421
+ };
3422
+ }
3423
+
3424
+ // good
3425
+ function foo() {
3426
+ return () => {
3427
+ console.log(this);
3428
+ };
3429
+ }
3430
+ ```
3431
+
3432
+ <a name="naming--filename-matches-export"></a><a name="22.6"></a>
3433
+
3434
+ - [23.6](#naming--filename-matches-export) A base filename should exactly match the name of its default export.
3435
+
3436
+ ```javascript static
3437
+ // file 1 contents
3438
+ class CheckBox {
3439
+ // ...
3440
+ }
3441
+ export default CheckBox;
3442
+
3443
+ // file 2 contents
3444
+ export default function fortyTwo() { return 42; }
3445
+
3446
+ // file 3 contents
3447
+ export default function insideDirectory() {}
3448
+
3449
+ // in some other file
3450
+ // bad
3451
+ import CheckBox from './checkBox'; // PascalCase import/export, camelCase filename
3452
+ import FortyTwo from './FortyTwo'; // PascalCase import/filename, camelCase export
3453
+ import InsideDirectory from './InsideDirectory'; // PascalCase import/filename, camelCase export
3454
+
3455
+ // bad
3456
+ import CheckBox from './check_box'; // PascalCase import/export, snake_case filename
3457
+ import forty_two from './forty_two'; // snake_case import/filename, camelCase export
3458
+ import inside_directory from './inside_directory'; // snake_case import, camelCase export
3459
+ import index from './inside_directory/index'; // requiring the index file explicitly
3460
+ import insideDirectory from './insideDirectory/index'; // requiring the index file explicitly
3461
+
3462
+ // good
3463
+ import CheckBox from './CheckBox'; // PascalCase export/import/filename
3464
+ import fortyTwo from './fortyTwo'; // camelCase export/import/filename
3465
+ import insideDirectory from './insideDirectory'; // camelCase export/import/directory name/implicit "index"
3466
+ // ^ supports both insideDirectory.js and insideDirectory/index.js
3467
+ ```
3468
+
3469
+ <a name="naming--camelCase-default-export"></a><a name="22.7"></a>
3470
+
3471
+ - [23.7](#naming--camelCase-default-export) Use camelCase when you export-default a function. Your filename should be identical to your function’s name.
3472
+
3473
+ ```javascript static
3474
+ function makeStyleGuide() {
3475
+ // ...
3476
+ }
3477
+
3478
+ export default makeStyleGuide;
3479
+ ```
3480
+
3481
+ <a name="naming--PascalCase-singleton"></a><a name="22.8"></a>
3482
+
3483
+ - [23.8](#naming--PascalCase-singleton) Use PascalCase when you export a constructor / class / singleton / function library / bare object.
3484
+
3485
+ ```javascript static
3486
+ const AirbnbStyleGuide = {
3487
+ es6: {},
3488
+ };
3489
+
3490
+ export default AirbnbStyleGuide;
3491
+ ```
3492
+
3493
+ <a name="naming--Acronyms-and-Initialisms"></a>
3494
+
3495
+ - [23.9](#naming--Acronyms-and-Initialisms) Acronyms and initialisms should always be all uppercased, or all lowercased.
3496
+
3497
+ > Why? Names are for readability, not to appease a computer algorithm.
3498
+
3499
+ ```javascript static
3500
+ // bad
3501
+ import SmsContainer from "./containers/SmsContainer";
3502
+
3503
+ // bad
3504
+ const HttpRequests = [
3505
+ // ...
3506
+ ];
3507
+
3508
+ // good
3509
+ import SMSContainer from "./containers/SMSContainer";
3510
+
3511
+ // good
3512
+ const HTTPRequests = [
3513
+ // ...
3514
+ ];
3515
+
3516
+ // also good
3517
+ const httpRequests = [
3518
+ // ...
3519
+ ];
3520
+
3521
+ // best
3522
+ import TextMessageContainer from "./containers/TextMessageContainer";
3523
+
3524
+ // best
3525
+ const requests = [
3526
+ // ...
3527
+ ];
3528
+ ```
3529
+
3530
+ <a name="naming--uppercase"></a>
3531
+
3532
+ - [23.10](#naming--uppercase) You may optionally uppercase a constant only if it (1) is exported, (2) is a `const` (it can not be reassigned), and (3) the programmer can trust it (and its nested properties) to never change.
3533
+
3534
+ > Why? This is an additional tool to assist in situations where the programmer would be unsure if a variable might ever change. UPPERCASE_VARIABLES are letting the programmer know that they can trust the variable (and its properties) not to change.
3535
+
3536
+ - What about all `const` variables? - This is unnecessary, so uppercasing should not be used for constants within a file. It should be used for exported constants however.
3537
+ - What about exported objects? - Uppercase at the top level of export (e.g. `EXPORTED_OBJECT.key`) and maintain that all nested properties do not change.
3538
+
3539
+ ```javascript static
3540
+ // bad
3541
+ const PRIVATE_VARIABLE =
3542
+ "should not be unnecessarily uppercased within a file";
3543
+
3544
+ // bad
3545
+ export const THING_TO_BE_CHANGED = "should obviously not be uppercased";
3546
+
3547
+ // bad
3548
+ export let REASSIGNABLE_VARIABLE = "do not use let with uppercase variables";
3549
+
3550
+ // ---
3551
+
3552
+ // allowed but does not supply semantic value
3553
+ export const apiKey = "SOMEKEY";
3554
+
3555
+ // better in most cases
3556
+ export const API_KEY = "SOMEKEY";
3557
+
3558
+ // ---
3559
+
3560
+ // bad - unnecessarily uppercases key while adding no semantic value
3561
+ export const MAPPING = {
3562
+ KEY: "value",
3563
+ };
3564
+
3565
+ // good
3566
+ export const MAPPING = {
3567
+ key: "value",
3568
+ };
3569
+ ```
3570
+
3571
+ **[⬆ back to top](#table-of-contents)**
3572
+
3573
+ ## Accessors
3574
+
3575
+ <a name="accessors--not-required"></a><a name="23.1"></a>
3576
+
3577
+ - [24.1](#accessors--not-required) Accessor functions for properties are not required.
3578
+
3579
+ <a name="accessors--no-getters-setters"></a><a name="23.2"></a>
3580
+
3581
+ - [24.2](#accessors--no-getters-setters) Do not use JavaScript getters/setters as they cause unexpected side effects and are harder to test, maintain, and reason about. Instead, if you do make accessor functions, use `getVal()` and `setVal('hello')`.
3582
+
3583
+ ```javascript static
3584
+ // bad
3585
+ class Dragon {
3586
+ get age() {
3587
+ // ...
3588
+ }
3589
+
3590
+ set age(value) {
3591
+ // ...
3592
+ }
3593
+ }
3594
+
3595
+ // good
3596
+ class Dragon {
3597
+ getAge() {
3598
+ // ...
3599
+ }
3600
+
3601
+ setAge(value) {
3602
+ // ...
3603
+ }
3604
+ }
3605
+ ```
3606
+
3607
+ <a name="accessors--boolean-prefix"></a><a name="23.3"></a>
3608
+
3609
+ - [24.3](#accessors--boolean-prefix) If the property/method is a `boolean`, use `isVal()` or `hasVal()`.
3610
+
3611
+ ```javascript static
3612
+ // bad
3613
+ if (!dragon.age()) {
3614
+ return false;
3615
+ }
3616
+
3617
+ // good
3618
+ if (!dragon.hasAge()) {
3619
+ return false;
3620
+ }
3621
+ ```
3622
+
3623
+ <a name="accessors--consistent"></a><a name="23.4"></a>
3624
+
3625
+ - [24.4](#accessors--consistent) It’s okay to create `get()` and `set()` functions, but be consistent.
3626
+
3627
+ ```javascript static
3628
+ class Jedi {
3629
+ constructor(options = {}) {
3630
+ const lightsaber = options.lightsaber || "blue";
3631
+ this.set("lightsaber", lightsaber);
3632
+ }
3633
+
3634
+ set(key, val) {
3635
+ this[key] = val;
3636
+ }
3637
+
3638
+ get(key) {
3639
+ return this[key];
3640
+ }
3641
+ }
3642
+ ```
3643
+
3644
+ **[⬆ back to top](#table-of-contents)**
3645
+
3646
+ ## Events
3647
+
3648
+ <a name="events--hash"></a><a name="24.1"></a>
3649
+
3650
+ - [25.1](#events--hash) When attaching data payloads to events (whether DOM events or something more proprietary like Backbone events), pass an object literal (also known as a "hash") instead of a raw value. This allows a subsequent contributor to add more data to the event payload without finding and updating every handler for the event. For example, instead of:
3651
+
3652
+ ```javascript static
3653
+ // bad
3654
+ $(this).trigger("listingUpdated", listing.id);
3655
+
3656
+ // ...
3657
+
3658
+ $(this).on("listingUpdated", (e, listingID) => {
3659
+ // do something with listingID
3660
+ });
3661
+ ```
3662
+
3663
+ prefer:
3664
+
3665
+ ```javascript static
3666
+ // good
3667
+ $(this).trigger("listingUpdated", { listingID: listing.id });
3668
+
3669
+ // ...
3670
+
3671
+ $(this).on("listingUpdated", (e, data) => {
3672
+ // do something with data.listingID
3673
+ });
3674
+ ```
3675
+
3676
+ **[⬆ back to top](#table-of-contents)**
3677
+
3678
+ ## jQuery
3679
+
3680
+ <a name="jquery--dollar-prefix"></a><a name="25.1"></a>
3681
+
3682
+ - [26.1](#jquery--dollar-prefix) Prefix jQuery object variables with a `$`.
3683
+
3684
+ ```javascript static
3685
+ // bad
3686
+ const sidebar = $(".sidebar");
3687
+
3688
+ // good
3689
+ const $sidebar = $(".sidebar");
3690
+
3691
+ // good
3692
+ const $sidebarBtn = $(".sidebar-btn");
3693
+ ```
3694
+
3695
+ <a name="jquery--cache"></a><a name="25.2"></a>
3696
+
3697
+ - [26.2](#jquery--cache) Cache jQuery lookups.
3698
+
3699
+ ```javascript static
3700
+ // bad
3701
+ function setSidebar() {
3702
+ $(".sidebar").hide();
3703
+
3704
+ // ...
3705
+
3706
+ $(".sidebar").css({
3707
+ "background-color": "pink",
3708
+ });
3709
+ }
3710
+
3711
+ // good
3712
+ function setSidebar() {
3713
+ const $sidebar = $(".sidebar");
3714
+ $sidebar.hide();
3715
+
3716
+ // ...
3717
+
3718
+ $sidebar.css({
3719
+ "background-color": "pink",
3720
+ });
3721
+ }
3722
+ ```
3723
+
3724
+ <a name="jquery--queries"></a><a name="25.3"></a>
3725
+
3726
+ - [26.3](#jquery--queries) For DOM queries use Cascading `$('.sidebar ul')` or parent > child `$('.sidebar > ul')`. [jsPerf](https://web.archive.org/web/20200414183810/https://jsperf.com/jquery-find-vs-context-sel/16)
3727
+
3728
+ <a name="jquery--find"></a><a name="25.4"></a>
3729
+
3730
+ - [26.4](#jquery--find) Use `find` with scoped jQuery object queries.
3731
+
3732
+ ```javascript static
3733
+ // bad
3734
+ $("ul", ".sidebar").hide();
3735
+
3736
+ // bad
3737
+ $(".sidebar").find("ul").hide();
3738
+
3739
+ // good
3740
+ $(".sidebar ul").hide();
3741
+
3742
+ // good
3743
+ $(".sidebar > ul").hide();
3744
+
3745
+ // good
3746
+ $sidebar.find("ul").hide();
3747
+ ```
3748
+
3749
+ **[⬆ back to top](#table-of-contents)**
3750
+
3751
+ ## ECMAScript 5 Compatibility
3752
+
3753
+ <a name="es5-compat--kangax"></a><a name="26.1"></a>
3754
+
3755
+ - [27.1](#es5-compat--kangax) Refer to [Kangax](https://twitter.com/kangax/)’s ES5 [compatibility table](https://kangax.github.io/es5-compat-table/).
3756
+
3757
+ **[⬆ back to top](#table-of-contents)**
3758
+
3759
+ <a name="ecmascript-6-styles"></a>
3760
+
3761
+ ## ECMAScript 6+ (ES 2015+) Styles
3762
+
3763
+ <a name="es6-styles"></a><a name="27.1"></a>
3764
+
3765
+ - [28.1](#es6-styles) This is a collection of links to the various ES6+ features.
3766
+
3767
+ 1. [Arrow Functions](#arrow-functions)
3768
+ 1. [Classes](#classes--constructors)
3769
+ 1. [Object Shorthand](#es6-object-shorthand)
3770
+ 1. [Object Concise](#es6-object-concise)
3771
+ 1. [Object Computed Properties](#es6-computed-properties)
3772
+ 1. [Template Strings](#es6-template-literals)
3773
+ 1. [Destructuring](#destructuring)
3774
+ 1. [Default Parameters](#es6-default-parameters)
3775
+ 1. [Rest](#es6-rest)
3776
+ 1. [Array Spreads](#es6-array-spreads)
3777
+ 1. [Let and Const](#references)
3778
+ 1. [Exponentiation Operator](#es2016-properties--exponentiation-operator)
3779
+ 1. [Iterators and Generators](#iterators-and-generators)
3780
+ 1. [Modules](#modules)
3781
+
3782
+ <a name="tc39-proposals"></a>
3783
+
3784
+ - [28.2](#tc39-proposals) Do not use [TC39 proposals](https://github.com/tc39/proposals) that have not reached stage 3.
3785
+
3786
+ > Why? [They are not finalized](https://tc39.github.io/process-document/), and they are subject to change or to be withdrawn entirely. We want to use JavaScript, and proposals are not JavaScript yet.
3787
+
3788
+ **[⬆ back to top](#table-of-contents)**
3789
+
3790
+ ## Standard Library
3791
+
3792
+ The [Standard Library](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects)
3793
+ contains utilities that are functionally broken but remain for legacy reasons.
3794
+
3795
+ <a name="standard-library--isnan"></a>
3796
+
3797
+ - [29.1](#standard-library--isnan) Use `Number.isNaN` instead of global `isNaN`.
3798
+ eslint: [`no-restricted-globals`](https://eslint.org/docs/rules/no-restricted-globals)
3799
+
3800
+ > Why? The global `isNaN` coerces non-numbers to numbers, returning true for anything that coerces to NaN.
3801
+ > If this behavior is desired, make it explicit.
3802
+
3803
+ ```javascript static
3804
+ // bad
3805
+ isNaN("1.2"); // false
3806
+ isNaN("1.2.3"); // true
3807
+
3808
+ // good
3809
+ Number.isNaN("1.2.3"); // false
3810
+ Number.isNaN(Number("1.2.3")); // true
3811
+ ```
3812
+
3813
+ <a name="standard-library--isfinite"></a>
3814
+
3815
+ - [29.2](#standard-library--isfinite) Use `Number.isFinite` instead of global `isFinite`.
3816
+ eslint: [`no-restricted-globals`](https://eslint.org/docs/rules/no-restricted-globals)
3817
+
3818
+ > Why? The global `isFinite` coerces non-numbers to numbers, returning true for anything that coerces to a finite number.
3819
+ > If this behavior is desired, make it explicit.
3820
+
3821
+ ```javascript static
3822
+ // bad
3823
+ isFinite("2e3"); // true
3824
+
3825
+ // good
3826
+ Number.isFinite("2e3"); // false
3827
+ Number.isFinite(parseInt("2e3", 10)); // true
3828
+ ```
3829
+
3830
+ **[⬆ back to top](#table-of-contents)**
3831
+
3832
+ ## Testing
3833
+
3834
+ <a name="testing--yup"></a><a name="28.1"></a>
3835
+
3836
+ - [30.1](#testing--yup) **Yup.**
3837
+
3838
+ ```javascript static
3839
+ function foo() {
3840
+ return true;
3841
+ }
3842
+ ```
3843
+
3844
+ <a name="testing--for-real"></a><a name="28.2"></a>
3845
+
3846
+ - [30.2](#testing--for-real) **No, but seriously**:
3847
+ - Whichever testing framework you use, you should be writing tests!
3848
+ - Strive to write many small pure functions, and minimize where mutations occur.
3849
+ - Be cautious about stubs and mocks - they can make your tests more brittle.
3850
+ - We primarily use [`mocha`](https://www.npmjs.com/package/mocha) and [`jest`](https://www.npmjs.com/package/jest) at Airbnb. [`tape`](https://www.npmjs.com/package/tape) is also used occasionally for small, separate modules.
3851
+ - 100% test coverage is a good goal to strive for, even if it’s not always practical to reach it.
3852
+ - Whenever you fix a bug, _write a regression test_. A bug fixed without a regression test is almost certainly going to break again in the future.
3853
+
3854
+ **[⬆ back to top](#table-of-contents)**
3855
+
3856
+ ## Performance
3857
+
3858
+ - [On Layout & Web Performance](https://www.kellegous.com/j/2013/01/26/layout-performance/)
3859
+ - [String vs Array Concat](https://web.archive.org/web/20200414200857/https://jsperf.com/string-vs-array-concat/2)
3860
+ - [Try/Catch Cost In a Loop](https://web.archive.org/web/20200414190827/https://jsperf.com/try-catch-in-loop-cost/12)
3861
+ - [Bang Function](https://web.archive.org/web/20200414205426/https://jsperf.com/bang-function)
3862
+ - [jQuery Find vs Context, Selector](https://web.archive.org/web/20200414200850/https://jsperf.com/jquery-find-vs-context-sel/164)
3863
+ - [innerHTML vs textContent for script text](https://web.archive.org/web/20200414205428/https://jsperf.com/innerhtml-vs-textcontent-for-script-text)
3864
+ - [Long String Concatenation](https://web.archive.org/web/20200414203914/https://jsperf.com/ya-string-concat/38)
3865
+ - [Are JavaScript functions like `map()`, `reduce()`, and `filter()` optimized for traversing arrays?](https://www.quora.com/JavaScript-programming-language-Are-Javascript-functions-like-map-reduce-and-filter-already-optimized-for-traversing-array/answer/Quildreen-Motta)
3866
+ - Loading...
3867
+
3868
+ **[⬆ back to top](#table-of-contents)**
3869
+
3870
+ ## Resources
3871
+
3872
+ **Learning ES6+**
3873
+
3874
+ - [Latest ECMA spec](https://tc39.github.io/ecma262/)
3875
+ - [ExploringJS](https://exploringjs.com/)
3876
+ - [ES6 Compatibility Table](https://kangax.github.io/compat-table/es6/)
3877
+ - [Comprehensive Overview of ES6 Features](http://es6-features.org/)
3878
+ - [JavaScript Roadmap](https://roadmap.sh/javascript)
3879
+
3880
+ **Read This**
3881
+
3882
+ - [Standard ECMA-262](https://www.ecma-international.org/ecma-262/6.0/index.html)
3883
+
3884
+ **Tools**
3885
+
3886
+ - Code Style Linters
3887
+ - [ESlint](https://eslint.org/) - [Airbnb Style .eslintrc](https://github.com/airbnb/javascript/blob/master/linters/.eslintrc)
3888
+ - [JSHint](https://jshint.com/) - [Airbnb Style .jshintrc](https://github.com/airbnb/javascript/blob/master/linters/.jshintrc)
3889
+ - Neutrino Preset - [@neutrinojs/airbnb](https://neutrinojs.org/packages/airbnb/)
3890
+
3891
+ **Other Style Guides**
3892
+
3893
+ - [Google JavaScript Style Guide](https://google.github.io/styleguide/jsguide.html)
3894
+ - [Google JavaScript Style Guide (Old)](https://google.github.io/styleguide/javascriptguide.xml)
3895
+ - [jQuery Core Style Guidelines](https://contribute.jquery.org/style-guide/js/)
3896
+ - [Principles of Writing Consistent, Idiomatic JavaScript](https://github.com/rwaldron/idiomatic.js)
3897
+ - [StandardJS](https://standardjs.com)
3898
+
3899
+ **Other Styles**
3900
+
3901
+ - [Naming this in nested functions](https://gist.github.com/cjohansen/4135065) - Christian Johansen
3902
+ - [Conditional Callbacks](https://github.com/airbnb/javascript/issues/52) - Ross Allen
3903
+ - [Popular JavaScript Coding Conventions on GitHub](http://sideeffect.kr/popularconvention/#javascript) - JeongHoon Byun
3904
+ - [Multiple var statements in JavaScript, not superfluous](https://benalman.com/news/2012/05/multiple-var-statements-javascript/) - Ben Alman
3905
+
3906
+ **Further Reading**
3907
+
3908
+ - [Understanding JavaScript Closures](https://javascriptweblog.wordpress.com/2010/10/25/understanding-javascript-closures/) - Angus Croll
3909
+ - [Basic JavaScript for the impatient programmer](https://www.2ality.com/2013/06/basic-javascript.html) - Dr. Axel Rauschmayer
3910
+ - [You Might Not Need jQuery](https://youmightnotneedjquery.com/) - Zack Bloom & Adam Schwartz
3911
+ - [ES6 Features](https://github.com/lukehoban/es6features) - Luke Hoban
3912
+ - [Frontend Guidelines](https://github.com/bendc/frontend-guidelines) - Benjamin De Cock
3913
+
3914
+ **Books**
3915
+
3916
+ - [JavaScript: The Good Parts](https://www.amazon.com/JavaScript-Good-Parts-Douglas-Crockford/dp/0596517742) - Douglas Crockford
3917
+ - [JavaScript Patterns](https://www.amazon.com/JavaScript-Patterns-Stoyan-Stefanov/dp/0596806752) - Stoyan Stefanov
3918
+ - [Pro JavaScript Design Patterns](https://www.amazon.com/JavaScript-Design-Patterns-Recipes-Problem-Solution/dp/159059908X) - Ross Harmes and Dustin Diaz
3919
+ - [High Performance Web Sites: Essential Knowledge for Front-End Engineers](https://www.amazon.com/High-Performance-Web-Sites-Essential/dp/0596529309) - Steve Souders
3920
+ - [Maintainable JavaScript](https://www.amazon.com/Maintainable-JavaScript-Nicholas-C-Zakas/dp/1449327680) - Nicholas C. Zakas
3921
+ - [JavaScript Web Applications](https://www.amazon.com/JavaScript-Web-Applications-Alex-MacCaw/dp/144930351X) - Alex MacCaw
3922
+ - [Pro JavaScript Techniques](https://www.amazon.com/Pro-JavaScript-Techniques-John-Resig/dp/1590597273) - John Resig
3923
+ - [Smashing Node.js: JavaScript Everywhere](https://www.amazon.com/Smashing-Node-js-JavaScript-Everywhere-Magazine/dp/1119962595) - Guillermo Rauch
3924
+ - [Secrets of the JavaScript Ninja](https://www.amazon.com/Secrets-JavaScript-Ninja-John-Resig/dp/193398869X) - John Resig and Bear Bibeault
3925
+ - [Human JavaScript](http://humanjavascript.com/) - Henrik Joreteg
3926
+ - [Superhero.js](http://superherojs.com/) - Kim Joar Bekkelund, Mads Mobæk, & Olav Bjorkoy
3927
+ - [JSBooks](https://jsbooks.revolunet.com/) - Julien Bouquillon
3928
+ - [Third Party JavaScript](https://www.manning.com/books/third-party-javascript) - Ben Vinegar and Anton Kovalyov
3929
+ - [Effective JavaScript: 68 Specific Ways to Harness the Power of JavaScript](https://amzn.com/dp/0321812182) - David Herman
3930
+ - [Eloquent JavaScript](https://eloquentjavascript.net/) - Marijn Haverbeke
3931
+ - [You Don’t Know JS: ES6 & Beyond](https://shop.oreilly.com/product/0636920033769.do) - Kyle Simpson
3932
+
3933
+ **Blogs**
3934
+
3935
+ - [JavaScript Weekly](https://javascriptweekly.com/)
3936
+ - [JavaScript, JavaScript...](https://javascriptweblog.wordpress.com/)
3937
+ - [Bocoup Weblog](https://bocoup.com/weblog)
3938
+ - [Adequately Good](https://www.adequatelygood.com/)
3939
+ - [NCZOnline](https://www.nczonline.net/)
3940
+ - [Perfection Kills](http://perfectionkills.com/)
3941
+ - [Ben Alman](https://benalman.com/)
3942
+ - [Dmitry Baranovskiy](http://dmitry.baranovskiy.com/)
3943
+ - [nettuts](https://code.tutsplus.com/?s=javascript)
3944
+
3945
+ **Podcasts**
3946
+
3947
+ - [JavaScript Air](https://javascriptair.com/)
3948
+ - [JavaScript Jabber](https://devchat.tv/js-jabber/)
3949
+
3950
+ **[⬆ back to top](#table-of-contents)**
3951
+
3952
+ ## In the Wild
3953
+
3954
+ This is a list of organizations that are using this style guide. Send us a pull request and we'll add you to the list.
3955
+
3956
+ - **123erfasst**: [123erfasst/javascript](https://github.com/123erfasst/javascript)
3957
+ - **4Catalyzer**: [4Catalyzer/javascript](https://github.com/4Catalyzer/javascript)
3958
+ - **Aan Zee**: [AanZee/javascript](https://github.com/AanZee/javascript)
3959
+ - **Airbnb**: [airbnb/javascript](https://github.com/airbnb/javascript)
3960
+ - **AloPeyk**: [AloPeyk](https://github.com/AloPeyk)
3961
+ - **AltSchool**: [AltSchool/javascript](https://github.com/AltSchool/javascript)
3962
+ - **Apartmint**: [apartmint/javascript](https://github.com/apartmint/javascript)
3963
+ - **Ascribe**: [ascribe/javascript](https://github.com/ascribe/javascript)
3964
+ - **Avant**: [avantcredit/javascript](https://github.com/avantcredit/javascript)
3965
+ - **Axept**: [axept/javascript](https://github.com/axept/javascript)
3966
+ - **Billabong**: [billabong/javascript](https://github.com/billabong/javascript)
3967
+ - **Bisk**: [bisk](https://github.com/Bisk/)
3968
+ - **Bonhomme**: [bonhommeparis/javascript](https://github.com/bonhommeparis/javascript)
3969
+ - **Brainshark**: [brainshark/javascript](https://github.com/brainshark/javascript)
3970
+ - **CaseNine**: [CaseNine/javascript](https://github.com/CaseNine/javascript)
3971
+ - **Cerner**: [Cerner](https://github.com/cerner/)
3972
+ - **Chartboost**: [ChartBoost/javascript-style-guide](https://github.com/ChartBoost/javascript-style-guide)
3973
+ - **Coeur d'Alene Tribe**: [www.cdatribe-nsn.gov](https://www.cdatribe-nsn.gov)
3974
+ - **ComparaOnline**: [comparaonline/javascript](https://github.com/comparaonline/javascript-style-guide)
3975
+ - **Compass Learning**: [compasslearning/javascript-style-guide](https://github.com/compasslearning/javascript-style-guide)
3976
+ - **DailyMotion**: [dailymotion/javascript](https://github.com/dailymotion/javascript)
3977
+ - **DoSomething**: [DoSomething/eslint-config](https://github.com/DoSomething/eslint-config)
3978
+ - **Digitpaint** [digitpaint/javascript](https://github.com/digitpaint/javascript)
3979
+ - **Drupal**: [www.drupal.org](https://git.drupalcode.org/project/drupal/blob/8.6.x/core/.eslintrc.json)
3980
+ - **Ecosia**: [ecosia/javascript](https://github.com/ecosia/javascript)
3981
+ - **Evernote**: [evernote/javascript-style-guide](https://github.com/evernote/javascript-style-guide)
3982
+ - **Evolution Gaming**: [evolution-gaming/javascript](https://github.com/evolution-gaming/javascript)
3983
+ - **EvozonJs**: [evozonjs/javascript](https://github.com/evozonjs/javascript)
3984
+ - **ExactTarget**: [ExactTarget/javascript](https://github.com/ExactTarget/javascript)
3985
+ - **Flexberry**: [Flexberry/javascript-style-guide](https://github.com/Flexberry/javascript-style-guide)
3986
+ - **Gawker Media**: [gawkermedia](https://github.com/gawkermedia/)
3987
+ - **General Electric**: [GeneralElectric/javascript](https://github.com/GeneralElectric/javascript)
3988
+ - **Generation Tux**: [GenerationTux/javascript](https://github.com/generationtux/styleguide)
3989
+ - **GoodData**: [gooddata/gdc-js-style](https://github.com/gooddata/gdc-js-style)
3990
+ - **GreenChef**: [greenchef/javascript](https://github.com/greenchef/javascript)
3991
+ - **Grooveshark**: [grooveshark/javascript](https://github.com/grooveshark/javascript)
3992
+ - **Grupo-Abraxas**: [Grupo-Abraxas/javascript](https://github.com/Grupo-Abraxas/javascript)
3993
+ - **Happeo**: [happeo/javascript](https://github.com/happeo/javascript)
3994
+ - **Honey**: [honeyscience/javascript](https://github.com/honeyscience/javascript)
3995
+ - **How About We**: [howaboutwe/javascript](https://github.com/howaboutwe/javascript-style-guide)
3996
+ - **HubSpot**: [HubSpot/javascript](https://github.com/HubSpot/javascript)
3997
+ - **Hyper**: [hyperoslo/javascript-playbook](https://github.com/hyperoslo/javascript-playbook/blob/master/style.md)
3998
+ - **InterCity Group**: [intercitygroup/javascript-style-guide](https://github.com/intercitygroup/javascript-style-guide)
3999
+ - **Jam3**: [Jam3/Javascript-Code-Conventions](https://github.com/Jam3/Javascript-Code-Conventions)
4000
+ - **JSSolutions**: [JSSolutions/javascript](https://github.com/JSSolutions/javascript)
4001
+ - **Kaplan Komputing**: [kaplankomputing/javascript](https://github.com/kaplankomputing/javascript)
4002
+ - **KickorStick**: [kickorstick](https://github.com/kickorstick/)
4003
+ - **Kinetica Solutions**: [kinetica/javascript](https://github.com/kinetica/Javascript-style-guide)
4004
+ - **LEINWAND**: [LEINWAND/javascript](https://github.com/LEINWAND/javascript)
4005
+ - **Lonely Planet**: [lonelyplanet/javascript](https://github.com/lonelyplanet/javascript)
4006
+ - **M2GEN**: [M2GEN/javascript](https://github.com/M2GEN/javascript)
4007
+ - **Mighty Spring**: [mightyspring/javascript](https://github.com/mightyspring/javascript)
4008
+ - **MinnPost**: [MinnPost/javascript](https://github.com/MinnPost/javascript)
4009
+ - **MitocGroup**: [MitocGroup/javascript](https://github.com/MitocGroup/javascript)
4010
+ - **Muber**: [muber](https://github.com/muber/)
4011
+ - **National Geographic Society**: [natgeosociety](https://github.com/natgeosociety/)
4012
+ - **NullDev**: [NullDevCo/JavaScript-Styleguide](https://github.com/NullDevCo/JavaScript-Styleguide)
4013
+ - **Nulogy**: [nulogy/javascript](https://github.com/nulogy/javascript)
4014
+ - **Orange Hill Development**: [orangehill/javascript](https://github.com/orangehill/javascript)
4015
+ - **Orion Health**: [orionhealth/javascript](https://github.com/orionhealth/javascript)
4016
+ - **Peerby**: [Peerby/javascript](https://github.com/Peerby/javascript)
4017
+ - **Pier 1**: [Pier1/javascript](https://github.com/pier1/javascript)
4018
+ - **Qotto**: [Qotto/javascript-style-guide](https://github.com/Qotto/javascript-style-guide)
4019
+ - **React**: [reactjs.org/docs/how-to-contribute.html#style-guide](https://reactjs.org/docs/how-to-contribute.html#style-guide)
4020
+ - **REI**: [reidev/js-style-guide](https://github.com/rei/code-style-guides/)
4021
+ - **Ripple**: [ripple/javascript-style-guide](https://github.com/ripple/javascript-style-guide)
4022
+ - **Sainsbury’s Supermarkets**: [jsainsburyplc](https://github.com/jsainsburyplc)
4023
+ - **Shutterfly**: [shutterfly/javascript](https://github.com/shutterfly/javascript)
4024
+ - **Sourcetoad**: [sourcetoad/javascript](https://github.com/sourcetoad/javascript)
4025
+ - **Springload**: [springload](https://github.com/springload/)
4026
+ - **StratoDem Analytics**: [stratodem/javascript](https://github.com/stratodem/javascript)
4027
+ - **SteelKiwi Development**: [steelkiwi/javascript](https://github.com/steelkiwi/javascript)
4028
+ - **StudentSphere**: [studentsphere/javascript](https://github.com/studentsphere/guide-javascript)
4029
+ - **SwoopApp**: [swoopapp/javascript](https://github.com/swoopapp/javascript)
4030
+ - **SysGarage**: [sysgarage/javascript-style-guide](https://github.com/sysgarage/javascript-style-guide)
4031
+ - **Syzygy Warsaw**: [syzygypl/javascript](https://github.com/syzygypl/javascript)
4032
+ - **Target**: [target/javascript](https://github.com/target/javascript)
4033
+ - **Terra**: [terra](https://github.com/cerner?utf8=%E2%9C%93&q=terra&type=&language=)
4034
+ - **TheLadders**: [TheLadders/javascript](https://github.com/TheLadders/javascript)
4035
+ - **The Nerdery**: [thenerdery/javascript-standards](https://github.com/thenerdery/javascript-standards)
4036
+ - **Tomify**: [tomprats](https://github.com/tomprats)
4037
+ - **Traitify**: [traitify/eslint-config-traitify](https://github.com/traitify/eslint-config-traitify)
4038
+ - **T4R Technology**: [T4R-Technology/javascript](https://github.com/T4R-Technology/javascript)
4039
+ - **UrbanSim**: [urbansim](https://github.com/urbansim/)
4040
+ - **VoxFeed**: [VoxFeed/javascript-style-guide](https://github.com/VoxFeed/javascript-style-guide)
4041
+ - **WeBox Studio**: [weboxstudio/javascript](https://github.com/weboxstudio/javascript)
4042
+ - **Weggo**: [Weggo/javascript](https://github.com/Weggo/javascript)
4043
+ - **Zillow**: [zillow/javascript](https://github.com/zillow/javascript)
4044
+ - **ZocDoc**: [ZocDoc/javascript](https://github.com/ZocDoc/javascript)
4045
+
4046
+ **[⬆ back to top](#table-of-contents)**
4047
+
4048
+ ## Translation
4049
+
4050
+ This style guide is also available in other languages:
4051
+
4052
+ - ![br](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/Brazil.png) **Brazilian Portuguese**: [armoucar/javascript-style-guide](https://github.com/armoucar/javascript-style-guide)
4053
+ - ![bg](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/Bulgaria.png) **Bulgarian**: [borislavvv/javascript](https://github.com/borislavvv/javascript)
4054
+ - ![ca](https://raw.githubusercontent.com/fpmweb/javascript-style-guide/master/img/catala.png) **Catalan**: [fpmweb/javascript-style-guide](https://github.com/fpmweb/javascript-style-guide)
4055
+ - ![cn](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/China.png) **Chinese (Simplified)**: [lin-123/javascript](https://github.com/lin-123/javascript)
4056
+ - ![tw](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/Taiwan.png) **Chinese (Traditional)**: [jigsawye/javascript](https://github.com/jigsawye/javascript)
4057
+ - ![fr](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/France.png) **French**: [nmussy/javascript-style-guide](https://github.com/nmussy/javascript-style-guide)
4058
+ - ![de](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/Germany.png) **German**: [timofurrer/javascript-style-guide](https://github.com/timofurrer/javascript-style-guide)
4059
+ - ![it](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/Italy.png) **Italian**: [sinkswim/javascript-style-guide](https://github.com/sinkswim/javascript-style-guide)
4060
+ - ![jp](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/Japan.png) **Japanese**: [mitsuruog/javascript-style-guide](https://github.com/mitsuruog/javascript-style-guide)
4061
+ - ![kr](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/South-Korea.png) **Korean**: [ParkSB/javascript-style-guide](https://github.com/ParkSB/javascript-style-guide)
4062
+ - ![ru](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/Russia.png) **Russian**: [leonidlebedev/javascript-airbnb](https://github.com/leonidlebedev/javascript-airbnb)
4063
+ - ![es](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/Spain.png) **Spanish**: [paolocarrasco/javascript-style-guide](https://github.com/paolocarrasco/javascript-style-guide)
4064
+ - ![th](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/Thailand.png) **Thai**: [lvarayut/javascript-style-guide](https://github.com/lvarayut/javascript-style-guide)
4065
+ - ![tr](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/Turkey.png) **Turkish**: [eraycetinay/javascript](https://github.com/eraycetinay/javascript)
4066
+ - ![ua](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/Ukraine.png) **Ukrainian**: [ivanzusko/javascript](https://github.com/ivanzusko/javascript)
4067
+ - ![vn](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/Vietnam.png) **Vietnam**: [dangkyokhoang/javascript-style-guide](https://github.com/dangkyokhoang/javascript-style-guide)
4068
+
4069
+ ## The JavaScript Style Guide Guide
4070
+
4071
+ - [Reference](https://github.com/airbnb/javascript/wiki/The-JavaScript-Style-Guide-Guide)
4072
+
4073
+ ## Chat With Us About JavaScript
4074
+
4075
+ - Find us on [gitter](https://gitter.im/airbnb/javascript).
4076
+
4077
+ ## Contributors
4078
+
4079
+ - [View Contributors](https://github.com/airbnb/javascript/graphs/contributors)
4080
+
4081
+ ## License
4082
+
4083
+ (The MIT License)
4084
+
4085
+ Copyright (c) 2012 Airbnb
4086
+
4087
+ Permission is hereby granted, free of charge, to any person obtaining
4088
+ a copy of this software and associated documentation files (the
4089
+ 'Software'), to deal in the Software without restriction, including
4090
+ without limitation the rights to use, copy, modify, merge, publish,
4091
+ distribute, sublicense, and/or sell copies of the Software, and to
4092
+ permit persons to whom the Software is furnished to do so, subject to
4093
+ the following conditions:
4094
+
4095
+ The above copyright notice and this permission notice shall be
4096
+ included in all copies or substantial portions of the Software.
4097
+
4098
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
4099
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
4100
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
4101
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
4102
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
4103
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
4104
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
4105
+
4106
+ **[⬆ back to top](#table-of-contents)**
4107
+
4108
+ ## Amendments
4109
+
4110
+ We encourage you to fork this guide and change the rules to fit your team’s style guide. Below, you may list some amendments to the style guide. This allows you to periodically update your style guide without having to deal with merge conflicts.
4111
+
4112
+