{"id":13556768,"url":"https://github.com/jonathanong/js-style-guide","last_synced_at":"2026-01-10T19:33:31.089Z","repository":{"id":26651345,"uuid":"30107530","full_name":"jonathanong/js-style-guide","owner":"jonathanong","description":"My style guide for JS","archived":true,"fork":false,"pushed_at":"2015-02-03T16:36:40.000Z","size":132,"stargazers_count":22,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-11-04T06:35:17.213Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jonathanong.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-01-31T08:56:35.000Z","updated_at":"2023-01-28T14:47:55.000Z","dependencies_parsed_at":"2022-08-20T19:50:37.481Z","dependency_job_id":null,"html_url":"https://github.com/jonathanong/js-style-guide","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonathanong%2Fjs-style-guide","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonathanong%2Fjs-style-guide/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonathanong%2Fjs-style-guide/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonathanong%2Fjs-style-guide/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jonathanong","download_url":"https://codeload.github.com/jonathanong/js-style-guide/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246984602,"owners_count":20864478,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T12:04:00.758Z","updated_at":"2025-04-03T10:31:31.397Z","avatar_url":"https://github.com/jonathanong.png","language":null,"funding_links":[],"categories":["Others","others"],"sub_categories":[],"readme":"\n# JavaScript Style Guide\n\nFor a purely aesthetic style guide, \nI recommend using [standard](https://github.com/feross/standard).\n\n## Organization\n\nWhenever possible, split your code into multiple files and modules!\nEach module should be defined:\n\n- From high level to low level - when reading the code, developers should immediately understand how the module works, then continue reading to learn the implementation details.\n- From public to private - when creating modules, the most important aspect is the `input -\u003e output`. This should be almost immediately obviously.\n\nCode in a single file should generally be structured in the following order:\n\n1. Global imports - `import lib from 'lib'`\n2. Relative imports - `import src from './src'`\n3. Exports - `export default Main`\n4. Variables - `var a = 1`\n5. Main - `function Main() {}`\n6. Prototype Properties - `Main.prototype.something = true`\n7. Utilities - `function add(a, b) {return a + b}`\n\n### Don't hoist variables!\n\nThere's no need for that!\nIt makes your code look messy and more difficult to understand.\nIt also demonstrates a lack of understanding of how JavaScript variables work.\n\n### Avoid defining (anonymous) functions within functions\n\nDoing so makes your code slower,\nbut refactoring anonymous functions into function declarations\nmakes your code easier to read.\n\n```js\nfunction sumArray(arr) {\n  return arr.reduce(function (a, b) {\n    return a + b\n  }, 0)\n}\n```\n\nshould really be:\n\n```js\nfunction sumArray(arr) {\n  return arr.reduce(sum, 0)\n}\n\nfunction sum(a, b) {\n  return a + b\n}\n```\n\nHowever, doing the same in the following example is unnecessary as a closure will always be required:\n\n```js\nfunction multiplyArray(x) {\n  return arr.map(function (y) {\n    return x * y\n  })\n}\n```\n\n### Move function declarations to the end of the closure and after the return statement\n\nName each function declaration something meaningful so that a developer knows what it does by its name.\nPut all the implementation details within that function.\n\n```js\nfunction () {\n  // do this\n  // do that\n  doTheNextThing()\n\n  return 'something'\n\n  function doTheNextThing() {\n\n  }\n}\n```\n\n## Performance\n\n### Avoid using `Function.prototype.bind`\n\nIt's slow and people are inevitably going to complain!\n\n### Avoid creating closures within functions\n\nHowever, this is the lesser of other evils, such as `.bind()`.\n\n### Minimize the number of variables within functions with closures\n\nSee this comment: https://github.com/jonathanong/style-guide/commit/ac8595b771d14e584e8b5e4a4b99ffac25aa29ae#commitcomment-7075319\n\n### Move try/catches to separate, small functions\n\nYou should be writing small functions anyways, but if there are any try/catches in a function,\ntry to move it out into a separate function.\n\nNote: in the future, this will not be relevant in v8\n\n## Anti-Patterns\n\n### Don't throw non-errors!\n\nStrings are not errors! If `!(err instanceof Error)`, then it's not an error!\nThe primary reason for this is so that errors have stack traces!\nOtherwise, where the hell did that error come from?\n\n### Avoid using node.js event emitters\n\nThe fact that they create uncaught exceptions if you do not add listeners to the `error` event is a major problem for beginners in node.js.\nIn general, if your API requires events, then it may be too complicated.\nTry making your API have a single input and single output.\n\n### Avoid using streams\n\nFor binary data, streams are fine, but you should in general abstract them into simpler functions.\nFor example, https://github.com/expressjs/body-parser abstracts requests to return the body of a request.\n\nOther than being a subclass of emitters, streams are very buggy in node.js and have many minor edge cases that may cause leaks if not handled properly.\n\nMy suggestion is to use many modules that abstract streams into `fn(stream) -\u003e result` results.\n\n### Don't use domains\n\nWhen abstracting emitters and streams, there is very little need to use domains.\nPlus, domains are about to be deprecated.\n\n## Git diffs\n\nWhen working with teams, you want to minimize git diffs so that you can properly assign blame.\nWhen deciding on a JS style guide, git diffs is really the only thing no one can argue against.\n\n### No trailing whitespace\n\nKeep it consistent!\n\n### Trailing line break\n\nMany editors such as `atom` have this enabled by default.\n\n### One var/const/let per line\n\nDon't do any crazy whitespacing with commas!\nOne declaration per line makes diffs easy!\n\n```js\nlet a = 1;\nlet b = 2;\n```\n\n### Define arrays and objects in multiple lines and with traililng commas\n\n```js\nvar thingsILike = [\n  'coffee',\n  'cigarettes',\n  'cocaine',\n]\n\nvar stuff = {\n  coffee: true,\n  cigarettes: true,\n  cocaine: true,\n}\n```\n\n### Do not define functions in an array\n\nDon't do this:\n\n```js\nvar fns = [function () {\n\n}, function () {\n\n}]\n```\n\nInstead, use function declarations:\n\n```js\nparallel([fn1, fn2, fn3], callback)\n\nfunction fn1() {}\nfunction fn2() {}\nfunction fn3() {}\n```\n\nOf course, use actually helpful function names. The same rule could apply to objects, but generally they aren't as difficult to read within objects.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjonathanong%2Fjs-style-guide","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjonathanong%2Fjs-style-guide","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjonathanong%2Fjs-style-guide/lists"}