--- title: Code example guidelines slug: MDN/Guidelines/Code_guidelines tags: - CSS - Code - Guide - Guidelines - HTML - JavaScript - MDN Meta - NeedsTranslation - Shell - TopicStub translation_of: MDN/Guidelines/Code_guidelines ---
{{MDNSidebar}}
{{IncludeSubnav("/en-US/docs/MDN")}}

This document series outlines the coding guidelines and best practices we use for writing demos, code snippets, interactive examples, etc, for use on MDN.

If you are looking for guidelines to follow when writing your code examples, you have come to the right place. The biggest advantage to adhering to these guidelines is that it will foster consistency across our samples and demos on MDN, which increases readability and comprehension overall.

Note: If you want advice on the styling of code as it appears on an MDN article, rather than the code content, see our Writing style guide.

Article structure

This article contains general high-level best practices for writing MDN code examples. Its subarticles are as follows:

General best practices

This section provides quick general best practices for creating an understandable minimal code sample to demonstrate usage of a specific feature or function.

Code samples need to be:

There is one overarching consideration that you need to keep in mind: Readers will copy and paste the code sample into their own code, and may put it into production.

Therefore, you need to make sure that the code example is usable and follows generally accepted best practices, and does not do anything that will cause an application to be insecure, grossly inefficient, bloated, or inaccessible. If the code example is not runnable or production-worthy, be sure to include a warning in a code comment and in the explanatory text — if it is a snippet and not a full example, make this clear. This also means that you should provide all of the information necessary to run the example including any dependencies and setup.

Code samples should be as self-contained and easy to understand as possible. The aim is not necessarily to produce efficient, clever code that impresses experts and has great functionality, but rather to produce reduced working examples that can be understood as quickly as possible.

Guidelines: