Language Fundamentals

JavaScript Syntax & Statements

JavaScript Syntax defines the rules for writing valid JavaScript code. Learn about statements, expressions, identifiers, keywords, and proper formatting conventions.

What are the basic syntax rules of JavaScript?

A JavaScript program is a series of statements, each usually ending with a semicolon. Code blocks are wrapped in { }. Names are case-sensitive (total and Total are different variables). Comments use // for one line and /* … */ for several lines.

// single-line comment
/* multi-line
   comment */
let total = 0;                 // statement
if (total === 0) {             // block
  console.log("Empty cart");
}

JavaScript Statements

A statement is a complete instruction that performs an action. JavaScript programs are sequences of statements executed by the browser.

Types of Statements

Declaration Statements

let, const, var, function

Expression Statements

Assignment, function calls, expressions with side effects

Control Flow Statements

if, for, while, switch

Statement Rules

Semicolons Optional

JavaScript has automatic semicolon insertion (ASI)

Whitespace Flexible

Spaces, tabs, and newlines are mostly ignored

Case Sensitive

myVariable and MyVariable are different

Best Practice

Use semicolons for clarity, even though they're optional. This prevents unexpected behavior from ASI.

Interactive Syntax Example

Code Editor

<pre id="out"></pre>

Preview

Key Points in This Example:

  • • Variable declarations create new identifiers
  • • Expression statements perform actions
  • • Comments document code without affecting execution
  • • Case sensitivity creates different variables

Identifiers & Naming

Identifiers are names used for variables, functions, classes, and other entities. JavaScript has specific rules for valid identifiers.

Valid Identifier Rules

✓

Must start with letter, $, or _

myVar, $element, _private
✓

Can contain letters, digits, $, _

user123, element$, var_name
✓

Case sensitive

myVar ≠ MyVar ≠ MYVAR

Invalid Examples

✗

Cannot start with digit

123var, 1stPlace
✗

Cannot use reserved words

let, const, function, if
✗

Cannot contain spaces or special chars

my var, user@name, data%

Naming Conventions

Following naming conventions makes your code more readable and maintainable. These are widely adopted standards in the JavaScript community.

camelCase

Variables and functions

PascalCase

Classes and constructors

UPPER_CASE

Constants

JavaScript Syntax: summary

Key points

  • Semicolons are mostly optional thanks to automatic semicolon insertion, but writing them, or using a formatter like Prettier, avoids rare bugs.
  • Identifiers can contain letters, digits, _ and $, but can't start with a digit.
  • Reserved words like class, return and function can't be used as variable names.
  • Whitespace and line breaks are mostly ignored, so format your code for readability.

Frequently asked questions

›Are semicolons required in JavaScript?

Usually not, because the engine inserts them automatically. A line starting with ( or [ can be joined to the previous line unexpectedly, so most teams either always use semicolons or let a formatter handle them.

›Is JavaScript case-sensitive?

Yes. myName, myname and MyName are three different identifiers.