Coding Conventions

Coding Conventions is section 22 of Beginner’s Guide to JavaScript.

JavaScript is a scripting language. So it does need to follow all the coding conventions like as any programming language. Coding conventions can be documented rules for teams to follow, or just be your individual coding practice.

So actually what is a coding convention?

Coding conventions are style guidelines for programming. They typically cover:

  • Naming and declaration rules for variables and functions.
  • Rules for the use of white space, indentation, and comments.
  • Programming practices and principles

Coding conventions secure software quality:

  • Improves code readability
  • Make code maintenance easier

If you follow this guideline, we’ll guaranteed your code will be a perfect one to maintain for a longer years.

Naming Conventions

  • Variable and function names written as camelCase
  • Global variable written in UPPERCASE
  • Constants (like PI) written in UPPERCASE

firstName = “John”;

lastName = “Doe”;

price = 19.90;
discount = 0.10;

fullPrice = price * 100 / discount;

Using hyp-hens, under_scores and dollar sign

Hyphens in HTML and CSS:

HTML5 attributes can start with data- (data-quantity, data-price). CSS uses hyphens in property-names (font-size). Therefor hyphens can be mistaken as subtraction attempts. Hyphens are not allowed in JavaScript names.


Many programmers prefer to use underscores (date_of_birth)

Dollar Sign

Don’t start names with a $ sign. It will put you in conflict with many JavaScript library names.



Put all declarations at the top of each script or function. This gives better, cleaner code, and reduces the possibility of accidental re-declarations. Since JavaScript automatically moves the declarations to the top anyway (JavaScript hoisting), it is always a good rule.

var firstName, lastName;
var price, discount, fullPrice;

firstName = “John”;
lastName = “Doe”;

price = 19.90;
discount = 0.10;

fullPrice = price * 100 / discount;

Spaces Around

Always put spaces around operators, and after commas for obtain perfect readability.

x = 5 + 6;        // Good
x=5+6             // Bad

[40, 100, 1, 5]   // Good
[40,100,1,5]      // Bad

Code Indentation

Always use 4 spaces for indentation of code blocks if you are not using a auto indentation support text editor.

function toCelsius(fahrenheit) {
    return (5/9) * (fahrenheit-32);

for (i = 1; i < 50; i++) {
sum += i;

Line Length

For readability, avoid lines longer than 80 characters. If a JavaScript statement does not fit on one line, the best place to break it, is after an operator or a comma. Most of the highend text editors format your codes according to this standard.

document.getElementById(“demo”).innerHTML =
    “Hello Dolly.”;


Coding conventions are not used by computers. Most rules have little impact on the execution of programs. Indentation and extra spaces are not significant in small scripts. For code in development, readability should be preferred. Larger production scripts should be minifyed.


What’s Next?

Learn what is Best Practices

Written by Sandeep Likhar

Sandeep Likhar is from India, where he is a blogger, eBook designer, and founder of LetsDnD. He has 6 years of experience in the industry as a Digital Publishing Expert and eBook Converter, providing services to authors, publishers, and distributors worldwide. He is proficient in converting books into various formats, such as HTML, epub, mobi, word, PDF, including all major online platforms like iTunes, Kobo, Kindle, CreateSpace, B&N, Smashwords, and more.

Comment Below