JavaScript Syntax
You already know a statement is one complete instruction, often ending with a semicolon. This session looks inside those instructions: syntax — the rules for writing values and names so the browser accepts your code.
What is JavaScript syntax and which pieces do beginners write first?
Syntax is the set of rules for writing JavaScript the engine can read. Early on you mainly write literals (fixed values: numbers, and text in quotes), identifiers (names you choose for things you will store later), and expressions (small recipes that produce a value, used inside a statement). JavaScript is case-sensitive: score and Score are different names. Keep opening and closing quotes matched for strings.
Syntax pieces at a glance
📊 Pieces of JavaScript syntax beginners meet first
| Piece | Example | What it means for you |
|---|---|---|
| Literal | 42 or "Hi" |
A value written directly in the code |
| String quotes | "Hello" or 'Hi' |
Marks text; opening and closing must match |
| Identifier | userName |
A name you invent (letters, digits, _, $; not starting with a digit) |
| Case sensitivity | total ≠ Total |
Same spelling, different capital letters → different names |
| Expression | 2 + 3 or "A" + "B" |
Builds a value that a statement can use |
| Statement (recap) | console.log(2 + 3); |
One complete instruction that *uses* syntax pieces |
Literals — values written in place
A literal is a value you type directly. Numbers need no quotes. Text (a string) sits between matching single or double quotes.
console.log(42);
console.log("Hello");
console.log('Also a string');
Habits for literals
- Numbers: write digits (and a decimal point when you need one)
- Strings: pick
"or'and close with the same kind - Do not put quotes around ordinary numbers unless you truly want text
Identifiers — names you invent
An identifier is a name for something in your program. You will use identifiers heavily when you learn variables next. For now, learn the spelling rules: start with a letter, _, or $; then letters, digits, _, or $. Avoid spaces and hyphens in names.
let city = "Tehran";
let year = 2026;
console.log(city);
console.log(year);
let introduces a name. The string and number on the right are literals. The names city and year are identifiers. Deep rules for declaring and updating values come in the Variables session — here the point is how names are written.
Case sensitivity
JavaScript treats capital letters as meaningful. color and Color are two different identifiers. Mismatching case is a common beginner bug: the code “looks right” but the name does not exist.
let color = "teal";
console.log(color);
// console.log(Color); // error if Color was never created
Expressions inside statements
An expression produces a value. A statement is the full instruction that does something with that value (for example logging it). You already write statements; syntax adds the pieces inside them.
console.log(2 + 3);
console.log("Hello" + " " + "world");
console.log(10 * 4);
2 + 3 is an expression (value 5). console.log(2 + 3); is a statement that prints that value. Operators such as + and * get their own sessions later; here you only need to see that expressions live inside statements.
Literals on the page
Visitors see values when a statement updates the page. The string you assign is still a literal — syntax and output work together.
<!DOCTYPE html>
<html>
<head>
<title>Syntax — Literal on page</title>
</head>
<body>
<h1 id="msg">Waiting…</h1>
<script>
document.getElementById("msg").textContent = "Syntax works";
</script>
</body>
</html>
The assignment statement ends with a semicolon. The right-hand side "Syntax works" is a string literal. Wrong quotes or a misspelled getElementById break the syntax or the name lookup.
Try it yourself
- Log a number literal and a string literal; then change both values and run again.
- Create
let label = "OK";, log it, then rename the identifier in both places so it still works. - Change
labeltoLabelin only one place and confirm the Console complains; fix the case. - Log
"Hi" + "!"and7 - 2; notice each expression sits inside a statement. - Open the HTML demo; change the quoted string assigned to
textContentand refresh.
Common mistakes
- Leaving a string without a matching closing quote
- Assuming
scoreandScoreare the same name - Starting an identifier with a digit (
1name) or using spaces in a name - Confusing this session with Statements: order and
{ }blocks were the last lesson; here the focus is values and names - Expecting the full Variables lesson already —
letappears only to show real identifiers
Summary
- Syntax is the rule set for writing valid JavaScript
- Literals are values written directly (numbers; quoted strings)
- Identifiers are names; spelling and case matter
- Expressions build values; statements use those values
- Next you will learn Comments: notes for humans that the engine ignores
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 Instructions
- Read each question carefully
- Select the best answer for each question
- You can retake the quiz as many times as you want
- Your progress will be shown at the top