JavaScript: JavaScript Syntax
Last updated: 2026-10-13
Syntax is the rules of the language — mastering JS syntax rules ensures your code is correctly understood by the browser.
1. Statements and Semicolons
JS code consists of statements, each performing an action. A semicolon ; marks the end of a statement.
Although JS has automatic semicolon insertion (ASI) and most code runs fine without semicolons, it's strongly recommended to add them manually. Why? Because ASI occasionally misinterprets your intent, causing hard-to-debug bugs. Building the habit of adding semicolons saves far more debugging time than the effort of typing them.
2. Code Blocks
Multiple statements wrapped in curly braces {} form a code block, commonly seen in function bodies, conditionals, and loops:
<script>
if (score > 60) {
result = "Pass";
color = "green";
}
</script>
Code blocks make multiple statements execute as a unit. Although braces can be omitted for single statements, never skip them — braces make your intent clearer and prevent bugs when you add more statements later.
3. Comments
Comments are written for humans; the browser ignores them entirely. Good comments explain "why," not "what" (the code itself should convey what it does).
- Single-line comment:
// comment - Multi-line comment:
/* comment */
4. Case Sensitivity
JS is strictly case-sensitive: myVar, myvar, and MYVAR are three completely different variables. Beginners often stumble here — the logic looks correct, but errors occur because of wrong casing.
5. Keywords and Reserved Words
JS reserves certain words as keywords (like var, let, const, function, if, else, etc.) and you cannot use them as variable names. The full list of reserved words is long; just remember the common ones — your editor's syntax highlighting will help identify them.
▶ Example: Statements, Semicolons, and Code Blocks
▶ Try it Yourself▶ Example: Using Comments
▶ Try it Yourself▶ Example: Case Sensitivity Demo
▶ Try it Yourself▶ Example: Naming Rules in Practice
▶ Try it Yourself❓ FAQ
return causes JS to auto-insert a semicolon, returning undefined instead of your intended value. These bugs are extremely sneaky — you might debug for hours only to find a missing semicolon. Why risk it?firstName) is the standard. snake_case (first_name) is more common in Python and database fields. When in Rome, do as the Romans do — use camelCase in JS.📖 Summary
- JS statements end with semicolons — always add them manually; don't rely on automatic semicolon insertion
- Code blocks use
{}— even for single statements, never omit the braces - Single-line comments use
//, multi-line comments use/* */— comments should explain "why," not "what" - JS is strictly case-sensitive —
myVar≠myvaris the most common beginner mistake - Variable names cannot start with digits, and cannot use keywords or reserved words
- camelCase is the recommended naming convention:
firstName,totalScore
📝 Exercises
- Basic: Write JS code that declares two variables
studentNameandstudentAge, assigns values to them, and prints them withconsole.log(). Ensure all statements have semicolons and variable names use camelCase. - Intermediate: Create a page with an input field and a button. When the user enters an age, determine if they're an adult (≥18) using an if/else code block, and display the result on the page.
- Challenge: Create a "Naming Checker" page where the user enters a variable name. JS checks if it's valid (no leading digits, no spaces or hyphens, not a keyword) and displays "Valid" or "Invalid" with the reason.