Curriculum
Course: JavaScript Basic
Login

Curriculum

JavaScript Basic

JSHome

0/216
Text lesson

JS Style Guide

JavaScript Coding Conventions

Coding conventions are guidelines for programming style, typically covering:

  • Naming and declaration rules for variables and functions.
  • Guidelines for white space, indentation, and comments.
  • Best practices and programming principles.

Coding conventions help ensure quality by:

  • Enhancing code readability.
  • Simplifying code maintenance.

These conventions can be formalized as documented rules for teams to follow or serve as personal coding practices.

Variable Names

At code7school, we use camelCase for naming identifiers (such as variables and functions).

All names begin with a letter.

Further details about naming rules can be found at the bottom of this page.

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

price = 19.90;
tax = 0.20;

fullPrice = price + (price * tax);

Spaces Around Operators

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

Examples:

let x = y + z;
const myArray = [“Volvo”“Saab”“Fiat”];

Code Indentation

Always use 2 spaces for indenting code blocks.

Functions:

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

Statement Rules

General rules for simple statements:

Always terminate a simple statement with a semicolon.

Examples:

const cars = [“Volvo”“Saab”“Fiat”];

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

General rules for complex (compound) statements:

 

  • Place the opening bracket at the end of the first line.
  • Leave 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.

Functions:

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

Loops:

for (let i = 0; i < 5; i++) {
  x += i;
}

Conditionals:

if (time < 20) {
  greeting = “Good day”;
else {
  greeting = “Good evening”;
}

Object Rules

General rules for defining objects:

 

  • Place the opening bracket on the same line as the object name.
  • Use a colon followed by one space between each property and its value.
  • Enclose string values in quotes, but not 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 terminate an object definition with a semicolon.

Example

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

Short objects can be written on a single line, with spaces between properties, like this:

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