JavaScript Syntax & Structure

Learn JavaScript syntax and structure, including statements, keywords, comments, and basic rules that every beginner must understand before writing JavaScript programs.

What is JavaScript Syntax?Link to this section

JavaScript syntax refers to the set of rules that define how JavaScript programs are written and interpreted by the browser or runtime environment. Syntax includes how statements are written, how variables are declared, how blocks are formed, and how instructions are executed. Writing correct syntax ensures that JavaScript code runs without errors.

JavaScript StatementsLink to this section

A JavaScript program is made up of statements. Each statement tells the browser to perform a specific action. Statements are executed line by line, from top to bottom.

note

Each statement performs one task. In this example, the task is printing text to the console

Semicolons in JavaScriptLink to this section

Semicolons (;) are used to separate JavaScript statements. JavaScript allows semicolons to be optional due to Automatic Semicolon Insertion (ASI), but using them is considered a good practice.

tip

Always use semicolons to avoid unexpected bugs and improve code readability.

JavaScript BlocksLink to this section

A block is a group of statements enclosed within curly braces {}. Blocks are commonly used in conditions, loops, and functions.

JavaScript KeywordsLink to this section

Keywords are reserved words in JavaScript that have special meanings and cannot be used as variable names. Examples of JavaScript keywords include:
  • let
  • const
  • var
  • if
  • else
  • function
  • return

warning

Using JavaScript keywords as variable names will result in syntax errors.

JavaScript is Case-SensitiveLink to this section

JavaScript treats uppercase and lowercase letters as different.

note

name and Name are treated as two different variables.

Whitespace and Line BreaksLink to this section

JavaScript ignores extra spaces and line breaks. Developers use whitespace to make code more readable. Readable code is easier to maintain and debug, especially in large applications.

tip

Use proper indentation to make your JavaScript code clean and professional.

Comments in JavaScriptLink to this section

Comments are used to explain code and are ignored by the JavaScript engine during execution.

Single-line Comments

Multi-line Comments

tip

Good comments improve code understanding for teams and future maintenance.
Check Your Understanding
Question 1 of 5

What is JavaScript syntax?