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.

Related Statements:console.log() function return const else let var

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:

HTML
<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).


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

Related Statements:console.log() function return const else let var

❓ FAQ

Q Can omitting semicolons really cause bugs?
A Most of the time no, but edge cases exist. For example, a line break after 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?
Q Should I add braces to single-line statements?
A Yes. Today it's one line; tomorrow you might add two more. Forgetting to add braces then causes logic errors. Braces cost a few bytes but save hours of debugging.
Q camelCase or snake_case — which is better?
A In the JS world, camelCase (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

📝 Exercises

  1. Basic: Write JS code that declares two variables studentName and studentAge, assigns values to them, and prints them with console.log(). Ensure all statements have semicolons and variable names use camelCase.
  2. 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.
  3. 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.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏