JavaScript Syntax Overview
Introduction
This chapter introduces the basic shape of JavaScript code—how files are organized, how statements are written, and which rules the engine enforces before you dive into variables and logic. Understanding syntax early prevents mysterious errors that are really just typos, missing braces, or invalid names.
Prerequisites
- Node.js LTS installed
- Completed Hello World in VS Code or terminal Hello World
- Familiarity with coding standards (naming and formatting)
JavaScript Source Files
- Programs are plain text files, usually ending in
.js - Node.js runs files with
node filename.js - Browsers load scripts via
<script>tags or bundled modules (covered later)
One file can contain many statements. The runtime executes them top to bottom unless control flow (loops, if, functions) redirects execution.
Statements and Semicolons
A statement is a complete instruction, such as a variable declaration or a function call.
// Each line is typically one statement
const appName = "learn-all";
console.log(appName);JavaScript allows Automatic Semicolon Insertion (ASI) in many places, so semicolons are often optional:
// Works without semicolons (common style in some teams)
const x = 1
const y = 2
console.log(x + y)Tip
Best Practice
Pick one semicolon style for your project (usually with semicolons) and let ESLint enforce it. Mixed style confuses readers and reviews.
Blocks with Curly Braces
Code grouped together uses { and }:
// Block groups statements under an if branch
const score = 85;
if (score >= 60) {
console.log("Pass");
console.log("Good job");
}Indentation is not enforced by the language (unlike Python), but 2-space indent is the most common convention—see coding standards.
Case Sensitivity
JavaScript treats names as case-sensitive:
const topic = "JavaScript";
// const Topic = "other"; // different variable — allowed
// console.log(TOPIC); // ReferenceError: TOPIC is not definedmyValue and myvalue are different identifiers. This catches many beginners when copying examples.
String Literals
JavaScript supports three common string forms:
| Form | Example |
|---|---|
| Single quotes | 'hello' |
| Double quotes | "hello" |
| Template literals | `Hello, ${name}` |
// Template literals (backticks) support embedded expressions
const name = "Ada";
const greeting = `Hello, ${name}!`;
console.log(greeting);Numbers and Booleans in Syntax
Numbers and booleans are literal values—you write them directly:
// Number and boolean literals
const maxRetries = 3;
const isReady = true;
const price = 19.99;Detailed type behavior appears in later chapters on data types and numbers.
Comments (Preview)
Comments are ignored at runtime. You will study them in depth in Comments:
// Single-line comment
console.log("visible");
/*
Multi-line comment
for longer notes
*/Strict Mode (Brief)
"use strict"; enables stricter rules (no silent errors for some mistakes). Modules and modern bundlers often run in strict mode automatically.
// File-level strict mode (top of file)
"use strict";
// const typo = 1;
// mistypedVar = 2; // ReferenceError in strict modeYou do not need to memorize every strict rule now—just know it exists for safer code.
A Minimal Runnable Example
Save as syntax-demo.js and run with node syntax-demo.js:
// Demonstrate core syntax pieces in one file
const language = "JavaScript";
if (language.length > 0) {
console.log(`Learning ${language} step by step.`);
}FAQ
Is JavaScript syntax similar to Java or C?
It shares some surface similarities ({}, ;, if), but types, objects, and the runtime model differ. Treat JavaScript as its own language.
Do I always need semicolons?
No, but teams often require them for consistency. Let ESLint decide for you.
Why does my code fail with Unexpected token?
Usually a missing }, ), or quote. Check the line above the reported error—syntax errors often originate earlier than the caret position.
Can one .js file contain multiple programs?
One file is one script context when run with node file.js. Real projects split code across modules (later chapters).
What comes after syntax basics?
Next: Output, then Statements, Comments, and Identifiers & Reserved Words.