Syntax

The rules that define correctly structured JavaScript programs.

Syntaxlet identifier = value;

Syntax is the set of rules for how a program is written. A JavaScript program is a list of instructions called statements.

Values and identifiers

  • Fixed values (literals) like 10 or 'Hello'.
  • Variable values stored using names (identifiers) like x or price.

JavaScript is case-sensitive: myName and myname are two different names.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Follow consistent semicolon and casing rules across a team codebase enforced by ESLint.
  • Avoid accidental variable shadowing by using proper block scoping with curly braces.
  • Write readable code by separating statements on individual lines per your style guide.

More examples

Case-sensitive identifiers

Demonstrates that JavaScript identifiers are case-sensitive, so userName and username are two distinct variables.

Example · js
let userName = "Alice";
let username = "bob";  // different variable
console.log(userName, username);

Semicolons and line breaks

Shows conventional use of semicolons to terminate statements, making the code unambiguous for parsers.

Example · js
const a = 10;
const b = 20;
const sum = a + b;
console.log(sum); // 30

Block scope with curly braces

Illustrates how curly braces define a block scope that limits where let-declared variables are accessible.

Example · js
{
  let blockVar = "only here";
  console.log(blockVar); // "only here"
}
// console.log(blockVar); // ReferenceError outside block

Discussion

  • Be the first to comment on this lesson.