</>
Skip to content
JavaScript lessons (6/64)

JavaScript — Syntax

The fixed vs the chosen

JavaScript syntax has two layers:

  • Fixed vocabulary — keywords the language owns: let, if, function, return… lowercase, unchangeable
  • Your names — variables and functions you invent (identifiers)
let price = 10;      // let = fixed · price = yours

Identifier rules

Names may contain letters, digits, _ and $, but:

let firstName = "Ada";    // ✓
let _temp = 1;            // ✓ underscore start allowed
let $total = 9;           // ✓ $ allowed (jQuery heritage)
let 2fast = true;         // ✗ can't start with a digit
let my name = "x";        // ✗ no spaces
let class = "math";       // ✗ reserved word

Reserved words (let if return new class function…) are permanently off-limits.

camelCase — the JS convention

// multi-word names: firstWordLowercase, then Capitalized words
let userName = "ada";
let totalPrice = 99;
function calculateTax() {}
const maxRetryCount = 3;

Other languages use snake_case or PascalCase; JavaScript's ecosystem standard is camelCase for variables/functions. Classes get PascalCase. Following conventions makes your code instantly readable to every other developer.

Statements and semicolons

Each instruction is a statement; semicolons separate them:

let a = 1;
let b = 2;
console.log(a + b);

Modern engines auto-insert missing semicolons, so both styles survive in the wild. Whichever team you join, stay consistent — mixed styles are the real crime.

Values and operators form expressions

Anything producing a value is an expression:

5 + 3              // 8
"ja" + "va"        // "java"
price * quantity   // depends on values

Expressions live inside statements:

let total = price * quantity;   // statement containing an expression

Case sensitivity bites everyone once

let result = compute();
console.log(Result);      // ReferenceError — capital R ≠ lower r

Every built-in too: getElementById (lower d!), toUpperCase, console.log.

Comments — two flavors

// line comment: quick notes, disabled code

/* block comment:
   spans multiple lines */

/**
 * JSDoc flavor — documents functions for tooling
 * @param {number} price
 */
function withTax(price) {
    return price * 1.2;
}

Comment the why; the code already shows the what.

A complete tiny program

// Convert prices with tax
const TAX_RATE = 0.2;

function addTax(amount) {
    return amount * (1 + TAX_RATE);
}

const base = 50;
console.log(`Total: $${addTax(base)}`);   // Total: $60

Constants in UPPER_SNAKE, camelCase helpers, template-literal output, one comment explaining intent — this is idiomatic modern JavaScript in nine lines.

Mini Practice

  1. Try creating let 9lives and let lives9 — learn which rule blocks which
  2. Rename three snake_case variables to camelCase
  3. Write a five-line program using a constant, a function, and a template literal
  4. Trigger a case-sensitivity error deliberately; read the message fully
  5. Add a JSDoc block above your function including a @param

Next: comments → (JS comments deep-dive)

Related Topics

Frequently Asked Questions about Syntax

What is Syntax in JavaScript?

Syntax is a fundamental concept in JavaScript. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn Syntax?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Syntax.

Why is Syntax important in JavaScript?

Syntax is essential for JavaScript development. Understanding this concept will help you write better code and solve real-world problems more effectively.