Episode 7 of 46

JavaScript Syntax

Learn the fundamental rules of JavaScript syntax — statements, semicolons, and comments.

Every language has rules about how code must be written. Let's learn the basic syntax rules of JavaScript.

Statements

A statement is a single instruction. Each statement typically ends with a semicolon:

let name = "Alice";
console.log(name);
let x = 5 + 3;

Semicolons

Semicolons are technically optional in JavaScript (it has "automatic semicolon insertion"), but it's best practice to always include them to avoid subtle bugs:

// Good — explicit semicolons
let a = 1;
let b = 2;

// Works, but risky — no semicolons
let a = 1
let b = 2

Comments

// This is a single-line comment

/*
   This is a
   multi-line comment
*/

let age = 25; // Inline comment

Case Sensitivity

JavaScript is case-sensitive. myName, MyName, and MYNAME are three different variables.

Naming Conventions

// camelCase — for variables and functions (most common)
let firstName = "Alice";
function getUserAge() { }

// PascalCase — for classes and constructors
class UserProfile { }

// UPPER_SNAKE_CASE — for constants
const MAX_RETRIES = 3;
const API_URL = "https://api.example.com";

Whitespace

JavaScript ignores extra spaces and line breaks. Use them to make your code readable:

// Both are identical to JavaScript
let x=5+3;
let x = 5 + 3;  // Much more readable!