JavaScript Syntax

Syntax is the set of rules for writing correct JavaScript. A JavaScript program is a list of statements, and each statement is an instruction for the computer.

let price = 100;
let tax = 18;
let total = price + tax;
console.log(total);
Output of the above program is

118


Rules to remember

  • Semicolons: a statement normally ends with ;. JavaScript can often add it for you, but write it to avoid surprises.
  • Case sensitive: name, Name and NAME are three different variables.
  • Whitespace: extra spaces and new lines are ignored. Use them to keep code readable.
  • Code blocks: statements that go together are wrapped in curly braces { }.
  • Strings are written in single quotes, double quotes or backticks.

Identifiers (names)

The names of variables and functions are called identifiers.

  • They can have letters, digits, _ and $.
  • They cannot start with a digit.
  • They cannot be a keyword like let, if or function.
let firstName = "Robin";     // camelCase, the usual style
let _count = 0;
let $price = 99;
// let 1name = "x";         // wrong: starts with a digit

The common style is camelCase: first word in small letters, then every new word with a capital letter, like totalPrice.


Case sensitivity

let age = 20;
console.log(age);
console.log(Age);
Output of the above program is

20
ReferenceError: Age is not defined


Summary

  • A program is a list of statements, usually ending with a semicolon.
  • JavaScript is case sensitive.
  • Use camelCase names that do not start with a digit.
  • Next: JavaScript Comments.

Congratulations! Chapter Finished. Learn more about the similar topics:
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.