learn2kode.in

JavaScript Syntax

JavaScript syntax is the set of rules that tells the browser how JavaScript code should be written and understood. Before learning advanced concepts, you must understand the basic building blocks of JavaScript syntax.

1. JavaScript Statements

A statement is a single line of code that performs an action.

Example
console.log("Welcome to JavaScript");

Each statement can end with a semicolon (;), but it’s optional. Beginners can use or skip semicolons — both work.

2. JavaScript Keywords

Keywords are reserved words that have a special meaning.

Example
Example

3. JavaScript is Case-Sensitive

Uppercase and lowercase letters are different.

Example
let Name = "John";
let name = "Peter";

These two variables are not the same.

4. Code Blocks { }

Code inside curly braces is considered a block.

Used in:
Example
if (true) {
  console.log("Inside block");
}

5. Whitespace and Line Breaks

JavaScript ignores extra spaces and line breaks. You can format your code for better readability.

Example

6. Comments in JavaScript

Comments are ignored by JavaScript and used to explain code.

Single-line comment:
// This prints hello
console.log("Hello");
Multi-line comment:
/*
This is a
multi-line comment
*/

7. Literals

Literals are fixed values written directly in the code.

Example
10            // number literal
"Learn2Kode"  // string literal
true          // boolean literal

8. Identifiers (Variable Names)

Identifiers are names used for variables, functions, etc.

Rules:
Example
let userName;
let _price;
let $amount;
Invalid:
let 1name; // ❌ cannot start with number

9. Expressions

An expression is a piece of code that produces a value.

5 + 3
x * 2
"Hello" + " World"
Example

10. Semicolons

JavaScript automatically inserts semicolons in most cases. Still, adding them manually reduces errors.

Example

Summary