JavaScript Best Practices

We've covered several introductory concepts for JavaScript programming in this course and how to debug your code. We'll review some coding guidelines and best practices you'll use as a JavaScript developer to ensure consistency, readability, security, and accessibility. Read this article to learn about some of the best practices for developing programs.

Good JavaScript examples on MDN

You can find good, concise, meaningful JavaScript snippets at the top of our JavaScript language reference pages – browse through it to find some.

Our interactive (and other) examples are generally written to follow the above guidelines, although be aware that they may differ in some places as they were mostly written before the guidelines were newly written.

For API examples, we'd like to highlight a few examples we think are good:

  • fetch() examples
  • fillRect() examples (the Canvas 2D examples are generally good, although they still use old-style var).
  • Payment Request API show() (The PaymentRequest examples are generally quite good).
  • Using the Web Audio API (general good practices for HTML, CSS, and JavaScript, and a good demonstration of how to use snippets and link to full examples elsewhere).
  • Using the Media Capabilities API (more general good practices for using code snippets in a guide).