Loading Please Wait...

Logo Lynxsia IT Solutions

JavaScript Style Guide

JS Style Guide

Always use the same coding conventions for all your JavaScript projects.

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.

Coding conventions secure quality:

  • Improve code readability
  • Make code maintenance easier

Coding conventions can be documented rules for teams to follow, or just be your individual coding practice.

Naming Conventions

Always use the same naming convention for all your code.

  • Variable and function names written as camelCase
  • Global variables and constant written in UPPERCASE
Underscores

Many programmers prefer to use underscores (date_of_birth), especially in SQL databases.

PascalCase

PascalCase is often preferred by C programmers.

camelCase

camelCase is used by JavaScript itself, by jQuery, and other JavaScript libraries.

					 
        
          first_name = "Lynxsia"; // underscore
          lastName = "IT Solutions";// camel case
          FullName = "Lynxsia IT Solutions"; // pascal case
        
      
Spaces Around Operators

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

					 
        
          let x = y + z;
          const myArray = ["Volvo", "Saab", "Fiat"];
        
      
Code Indentation

Always use indentation (2 or 4 spaces) of code blocks.

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

General rules for simple statements:

  • Always end a simple statement with a semicolon
					 
        
          const cars = ["Volvo", "Saab", "Fiat"];

          const person = {
            firstName: "John",
            lastName: "Doe",
            age: 50,
            eyeColor: "blue"
          };
        
      

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.
					 
        
          function toCelsius(fahrenheit) {
            return (5 / 9) * (fahrenheit - 32);
          }
        
      
Object Rules

General rules for object definitions:

  • Place the opening bracket on the same line as the object name.
  • Use colon plus one space between each property and its value.
  • Use quotes around string values, not around numeric values.
  • Do not add a comma after the last property-value pair.
  • Place the closing bracket on a new line, without leading spaces.
  • Always end an object definition with a semicolon.
					 
        
          // Short
          const person = {firstName:"John", lastName:"Doe"};

          // Long
          const person = {
            firstName: "John",
            lastName: "Doe",
            age: 50,
            eyeColor: "blue"
          };
        
      
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.

					 
        
          document.getElementById("demo").innerHTML =
          "Hello Dolly.";
        
      
Loading JavaScript in HTML

Use simple syntax for loading external scripts (the type attribute is not necessary).

					 
        
          <script src="myscript.js"></script>
        
      
Use Lower Case File Names

Most web servers (Apache, Unix) are case sensitive about file names. Always use lower case file names (if possible).

Performance

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 minimized.

How you feel about this blog:

Share this blog on:

Report Us

If you find any error in the turtorials, or want to share your suggestion/feedback, feel free to send us email at: info@lynxsia.com

Contact Us
Ads
Logo
Lynxsia IT Solutions

We are concern with various development process like website design & development, E-commerce development, Software development, Application development, SMS & Bulk SMS Provider, PWA Development, and many more..

Kotwali Road, Chhiptehri, Banda, 210001, UP, India

Copyright © 2022, Lynxsia IT Solutions, All rights reserved