Back to JavaScript Notes
Topic #273

JavaScript Syntax

By the end of this lesson, you can write valid JavaScript statements using values, operators, comments, and semicolons.

What it is

JavaScript syntax is the set of rules that tells the JavaScript engine how to read your code. A value is data such as 5, "hello", true, or null. An operator combines or changes values, such as +, -, *, /, =, ===, and &&. An expression produces a value, while a statement performs an action, such as declaring a variable or calling a function.

A useful mental model is language grammar: an expression is like a phrase, and a statement is like a complete sentence. For example, 2 + 3 is an expression, but let answer = 2 + 3; is a statement. Comments are notes for humans and are ignored by JavaScript. Related terms include declaration, assignment, block, and automatic semicolon insertion.

Why it matters

  • Correct syntax prevents SyntaxError messages that stop code from running.
  • Clear statements make programs easier to read, test, and maintain.
  • Comments help explain intent, especially in complex logic.
  • Understanding expressions and statements is required before learning loops, functions, and objects.
  • Consistent syntax reduces subtle bugs caused by missing punctuation or ambiguous line breaks.

Syntax or steps

The smallest useful JavaScript pattern is a variable declaration statement:

  1. Choose a declaration keyword: let for a value that may change, or const for a value that should not be reassigned.
  2. Give the variable a valid identifier, such as total or userName.
  3. Use = to assign an expression.
  4. End the statement with a semicolon, ;.
  5. Add comments when the code needs explanation.

Example

// Calculate total price with tax
const price = 20;
const taxRate = 0.08;
let total = price + price * taxRate;

console.log(total); // 21.6

Part by part:

  • // Calculate total price with tax is a single-line comment.
  • const price = 20; declares a constant named price and assigns the number 20.
  • const taxRate = 0.08; declares another constant for the tax rate.
  • let total = price + price * taxRate; declares a variable and assigns the result of an expression.
  • Multiplication happens before addition, so price * taxRate is calculated first.
  • console.log(total); is a statement that prints the value of total.

Common mistakes

  • Missing closing punctuation. For example, console.log(total; is invalid because the closing parenthesis is missing. Fix it by completing the expression and punctuation.
  • Using an invalid identifier. Names cannot start with a digit, so let 1st = 10; causes a syntax error. Use let first = 10; instead.
  • Confusing line comments with block comments. // comments only one line. For multiple lines, use /* ... */.
  • Relying too much on automatic semicolon insertion. JavaScript can sometimes insert semicolons for you, but this can create surprises when a line starts with ( or [. Use semicolons consistently.

When to use it

JavaScript syntax is used when you need code that a browser or Node.js can execute. Pseudocode is useful when you are planning logic before writing real code.

Approach Use when Example
JavaScript syntax You need runnable code. let total = price + tax;
Pseudocode You are designing logic or explaining steps. total = price + tax

Practice

Guided exercise: Write JavaScript statements that store hours = 8 and rate = 15, then calculate pay and print it.

Expected output:

120

Challenge: Fix this code so it runs and prints 15.

let x = 5;
let y = 10;
console.log(x + y;

Solution hint: the console.log call is missing a closing parenthesis.

Quick check

Which is a statement: x + 5 or let x = 5;?

let x = 5; is a statement because it performs an action. x + 5 is an expression because it produces a value.

Summary

JavaScript syntax is the grammar that turns values, operators, and comments into valid statements. Learning the difference between expressions and statements gives you a foundation for writing correct, readable JavaScript.

Want to go beyond the notes?

Join Coding Now Tech Institute's JavaScript course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

JavaScript Syntax – FAQs

Quick answers about learning JavaScript Syntax in JavaScript.

This free note from Coding Now Tech Institute explains JavaScript Syntax in JavaScript — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every JavaScript topic on Coding Now Tech Institute, including JavaScript Syntax, is 100% free with no signup required.
With focused practice, most students grasp JavaScript Syntax in 1–3 days from these notes; pairing it with Coding Now Tech Institute's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the Coding Now Tech Institute Community (/community) — expert instructors answer within 24 hours.
Call NowEnroll Now