JavaScript ยท Chapter 5 of 55

JavaScript Syntax

JavaScript syntax defines the rules for writing valid code: how values, operators, expressions, keywords and comments fit together.

JavaScript is case-sensitive, meaning `myVar` and `myvar` are different identifiers. It largely ignores extra whitespace, letting you format code for readability.

Values and identifiers

Literal values like numbers and strings are called values. Identifiers are names given to variables and functions; they must start with a letter, underscore, or dollar sign.

Keywords

Reserved words like `let`, `const`, `function`, `if`, `return` have special meaning and cannot be used as identifiers.

Example 1 (javascript)
let price = 19.99;
let productName = "Book";
console.log(productName, price);
Output
Book 19.99

Two variable declarations followed by a console output.

Example 2 (javascript)
let $el = "special";
let _private = "hidden";
console.log($el, _private);
Output
special hidden

Identifiers may start with $ or underscore.

Key points

  • JavaScript is case-sensitive.
  • Identifiers can include letters, digits, $ and _ but not start with a digit.
  • Reserved keywords cannot be used as variable names.
  • Extra whitespace and line breaks are generally ignored.
๐Ÿ’ก Note: Consistent formatting (via tools like Prettier) makes JS syntax much easier to read.

๐Ÿ“ Quick Quiz

1. Is JavaScript case-sensitive?

2. Which is a valid identifier?

3. Reserved keywords like `if` and `return`: