JavaScript: JavaScript Syntax

🗓 Last updated: 2026-11-30👤 Written by: Alex✓ Reviewed by: MOTO🏷 Category: JavaScript Tutorial

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 tag -->
<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 What is the difference between JavaScript and Java?
A They are completely different languages that just happen to have similar names. JavaScript is mainly used for web pages and front-end interaction, while Java is a standalone general-purpose programming language often used for back-end and enterprise systems. Their syntax, runtime environments, and purposes all differ, and you don't need to learn Java before learning JS.
Q Is JavaScript case-sensitive? What happens if I get the case wrong?
A JS is case-sensitive, so myName, myname, and MyName are three different identifiers. Variable names, function names, and keywords (such as if and return) must match case exactly; writing If or Return will cause a syntax error.
Q Can the semicolon at the end of a JavaScript statement be omitted?
A In most cases yes, since JS has automatic semicolon insertion, but not every line break gets a semicolon added. Edge cases like a line break after return or a new line starting with ( or [ can easily cause subtle bugs, so it's best to write semicolons explicitly and avoid relying on automatic insertion.

📖 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.

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

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

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