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.
A statement is a single line of code that performs an action.
console.log("Welcome to JavaScript");
Each statement can end with a semicolon (;), but it’s optional. Beginners can use or skip semicolons — both work.
Keywords are reserved words that have a special meaning.
let name = "learn2kode";
Uppercase and lowercase letters are different.
let Name = "John";
let name = "Peter";
These two variables are not the same.
Code inside curly braces is considered a block.
if (true) {
console.log("Inside block");
}
JavaScript ignores extra spaces and line breaks. You can format your code for better readability.
let a = 10;
let b = 20;
Comments are ignored by JavaScript and used to explain code.
// This prints hello
console.log("Hello");
/*
This is a
multi-line comment
*/
Literals are fixed values written directly in the code.
10 // number literal
"Learn2Kode" // string literal
true // boolean literal
Identifiers are names used for variables, functions, etc.
let userName;
let _price;
let $amount;
let 1name; // ❌ cannot start with number
An expression is a piece of code that produces a value.
5 + 3
x * 2
"Hello" + " World"
let total = 10 + 5;
JavaScript automatically inserts semicolons in most cases. Still, adding them manually reduces errors.
let a = 10;
let b = 20;