JavaScript syntax defines how we write statements, expressions, identifiers, comments, and literals. Mastering these basics makes later topics like variables, functions, and DOM much easier.
JavaScript Syntax
1) Statements & Semicolons (ASI)
Each instruction is a statement. Semicolons are optional because of Automatic Semicolon Insertion (ASI), but keeping semicolons avoids rare edge cases.
console.log('Hello'); // statement 1
console.log('World'); // statement 2
(, [, +, -, etc. can join with the previous line unexpectedly. Keep semicolons or begin such lines with one.2) Comments
Use comments to document intent or temporarily disable code.
// Single-line comment
console.log('Visible'); // runs
/* Multi-line
comment block */
3) Identifiers & Keywords
Identifiers (names) can contain letters, digits (not first), _, $. They are case-sensitive. Reserved keywords cannot be used as variable names.
let userName = 'Sonu'; // ok
let $count = 10, _total1 = 20; // ok
// let class = 1; // ❌ reserved keyword
4) Whitespace & New Lines
Whitespace usually doesn’t matter. Format code for readability. Avoid ASI pitfalls by using semicolons.
let a=1, b=2;
console.log(
a + b
); // 3
5) Code Blocks { ... } (Scope Preview)
Blocks group statements. Variables declared with let/const are block-scoped; we’ll go deeper on the next page.
if (true) {
let msg = 'inside';
console.log(msg);
}
// msg is not accessible here
6) Literals (Numbers, Strings, Booleans, null, undefined)
Write values directly with literals. Numbers are 64-bit floating point; strings are in quotes; booleans are true/false.
const n = 42, pi = 3.14;
const s1 = 'single', s2 = "double";
const ok = true, bad = false;
const empty = null, missing = undefined;
7) Strings: Escaping & Template Literals
Escape characters with backslashes; use backticks (`) for interpolation and multi-line strings.
const quote = 'It\'s fine';
const name = 'Sonu';
const msg = `Hello, ${name}! 2+2=${2+2}`;
8) Expressions vs Statements
An expression produces a value; a statement performs an action. Many statements contain expressions.
// expression:
(2 + 3) * 4
// statement (declaration + expression):
let sum = 2 + 3;
9) Strict Mode
Add 'use strict' at the top of a file or function to catch common mistakes. ES modules are strict by default.
'use strict';
// Following would throw ReferenceError (x not declared):
// x = 10;
10) Script Placement & defer/async
Put scripts before </body> or use defer/async so HTML parses first.
<!-- Recommended: parsed first, executed after HTML -->
<script src="app.js" defer></script>
<!-- async runs as soon as it downloads (order may vary) -->
<script src="analytics.js" async></script>
11) Reserved Words (Cannot Be Identifiers)
Some words are part of the language grammar and cannot be used as variable names.
// Examples: let, const, var, function, class, return, if, else, switch, case,
// default, for, while, do, break, continue, try, catch, finally, throw,
// import, export, new, delete, typeof, instanceof, in, of, await, yield
let and const.