0% found this document useful (0 votes)
7 views

JavaScriptStyle

Js scripts
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

JavaScriptStyle

Js scripts
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

JavaScript Style Guide and

Coding Conventions
https://www.w3schools.com/js/js_conventions.asp

https://www.w3schools.com/js/js_best_practices.asp

JavaScript Coding Conventions


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

Variable Names
Use camelCase for identifier names (variables and functions).

Camel case convention means typing the words in lower-case, only capitalizing the first
letter in each word.

All names start with a letter.

Spaces Around Operators


Always put spaces around operators ( = + - * / ), and after commas:

Code Indentation
Always use 2 spaces for indentation of code blocks:
Statement Rules
General rules for simple statements:

• Always end a simple statement with a semicolon.

General rules for complex (compound) statements:

• Put the opening bracket at the end of the first line.


• Use one space before the opening bracket.
• Put the closing bracket on a new line, without leading spaces.
• Do not end a complex statement with a semicolon.

Line Length < 80


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.

Naming Conventions
Always use the same naming convention for all your code. For example:

• Variable and function names written as camelCase


• Camel case convention, typing the words in lower-case, only capitalizing the first
letter in each word.
• Global variables written in UPPERCASE
• Constants (like PI) written in UPPERCASE

Hyphens are not allowed in JavaScript names.

File Extensions
HTML files should have a .html extension (not .htm).

CSS files should have a .css extension.

JavaScript files should have a .js extension.


Use Lower Case File Names
Initialize Variables
It is a good coding practice to initialize variables when you declare them.

This will:

• Give cleaner code


• Provide a single place to initialize variables
• Avoid undefined values

You might also like