site logo

Master JavaScript — Build, Create, and Innovate


Category: (All)
❮  Go Back

02. JavaScript Syntax, Statements & Comments: A Beginner’s Guide

Introduction

When learning JavaScript, three foundational concepts are syntax, statements, and comments. These define how you write valid JavaScript code, how the browser executes instructions, and how you document or disable parts of your code. In this guide, you will learn:

  1. What JavaScript syntax rules are
  2. How statements form the building blocks of JS programs
  3. How and when to use comments to improve readability or test code


1. JavaScript Syntax: The Rules of the Language

1.1 What Is Syntax?

Syntax refers to the set of rules that define how JavaScript code must be written for the interpreter (browser or engine) to understand it. Just like grammar rules in a language, breaking syntax rules results in errors or unexpected behavior.

1.2 Key Syntax Elements

  1. Literals and variables
  2. A literal is a fixed value, such as 42, "Hello", true, or null.
  3. A variable is a named container for data. You declare variables with let, const, or (less commonly today) var.
  4. Identifiers
  5. The name you choose for a variable or function is an identifier.
  6. Rules for identifiers: start with a letter, underscore _, or dollar sign $; following characters can include letters, digits, underscores; cannot be a reserved keyword.
  7. Operators and expressions
  8. Operators like +, -, *, /, =, etc. are used to perform operations.
  9. An expression is any valid combination of variables, literals, and operators that evaluates to a value.
  10. Case sensitivity
  11. JavaScript is case sensitive — for example, myVar and myvar are distinct.
  12. Semicolons and statement termination
  13. Statements typically end with a semicolon (;) to mark the end of the command.
  14. JavaScript does allow omitting semicolons in many cases (because of “automatic semicolon insertion”), but using them is a good practice.
  15. Whitespace and formatting
  16. Extra spaces, tabs, or line breaks are generally ignored. They help make code readable.
  17. You can break statements across lines (after operators or commas) for readability.
  18. Code blocks
  19. Curly braces {} are used to group statements together — for example, in functions, loops, conditionals.


2. JavaScript Statements: Building Blocks of a Program

2.1 What Is a Statement?

A JavaScript statement is an instruction or command that tells the browser to do something. A full program is a sequence of statements executed one after another.

For example:

let a = 5;
let b = 10;
let sum = a + b;

Each of those lines is a statement.

2.2 Components of Statements

Statements are composed of the following parts:

  1. Values (literals or variables)
  2. Operators (e.g. =, +)
  3. Expressions
  4. Keywords (reserved words like let, if, while)
  5. Comments (which can be part of or appended to a statement)

2.3 Semicolons & Multiple Statements on a Line

  1. You can separate multiple statements on a single line using semicolons:
a = 5; b = 10; sum = a + b;
  1. Even though omitting semicolons is allowed, many developers prefer to include them for clarity and to avoid edge case pitfalls.

2.4 Grouping Statements in Blocks

Statements can be grouped together inside { }. Blocks are often used in conditionals, loops, functions, etc.

2.5 Example of Statements in Action

if (hour < 18) {
greeting = "Good day";
} else {
greeting = "Good evening";
}

Here each line inside the if or else block is a statement. The if, else keywords direct which block executes.


3. JavaScript Comments: Documenting and Controlling Code

Comments are non-executing portions of code used to explain or temporarily disable code.

3.1 Single-Line Comments

Start with //. Everything after // to the end of that line is ignored by JavaScript.

Example:

// This is a single-line comment
let x = 5; // comment at end of a statement


3.2 Multi-Line (Block) Comments

Start with /* and end with */. They can span multiple lines.

Example:

/*
This block comment spans multiple lines.
It is useful to explain larger segments of code.
*/
let y = 10;

3.3 Using Comments to Disable Code

You can comment out code temporarily during debugging:

// document.write("This will not run");

or block out several lines:

/*
let x = 1;
let y = 2;
let sum = x + y;
*/


3.4 Best Practices for Comments

  1. Use comments to explain why a piece of code exists, not just what it does.
  2. Avoid over-commenting trivial code (letting clean code speak for itself).
  3. Keep comments up to date when the code changes.
  4. Use multi-line comments sparingly and for more formal documentation or longer explanations.


Conclusion

Understanding JavaScript syntax, statements, and comments is essential to writing correct, maintainable, and readable code. Syntax gives you the rules, statements are the executable instructions, and comments help document and manage your code. Once you grasp these fundamentals, you will be better equipped to progress to functions, control flow, DOM manipulation, and beyond.

02. JavaScript Syntax Statements Comments A Beginners Guide

coldshadow44 on 2025-10-12



(0)





Showing comments related to this post.




Member's Sites: