CA Resources

General Rules

Master essential JavaScript writing rules, including semicolon usage, spacing, and statement formatting.

General Rules in Writing JavaScript

Semicolons (;) are used to separate JavaScript statement therefore, every statement should have a semicolon at the end.

document.write("Hello everyone");

Multiple statements can be allowed to be written in one line when separated by semicolons.

a = 5; b = 6; c = a + b;

JavaScript ignores multiple spaces. Users can add white space to the script to make it more readable.

let person="Max";
 
// is the same as
let person = "Max";

A good practice is to put spaces around operators (= + - * /).

let x = y + z;

We can use the + symbol for concatenation.

window.alert("My age is " + 12);

We can insert HTML codes in JavaScript outputs.

document.write("<h1>Hello</h1>");

JavaScript code can be grouped together in blocks by placing them inside curly brackets.

function myFunction() {
  document.getElementById("demo1").innerHTML = "Hello everyone!";
  document.getElementById("demo2").innerHTML = "How are you?";
}
Edit on GitHub

Last updated on

On this page