JavaScript syntax is the set of rules that defines a correctly structured JavaScript program. It controls how we write instructions so that the browser can understand and execute them.
Once you know the basic syntax—values, identifiers, statements, semicolons, and code blocks—you can start writing valid JavaScript programs with confidence.
let, const, or var.{ }.In JavaScript, every piece of logic is written as statements using values and variables. Statements are grouped into blocks and organised using control structures (like if and for).
To keep code readable and error-free, follow the rules for identifiers, use semicolons consistently, and always match opening and closing braces.
JavaScript values can be fixed values (literals) or variable values (stored in variables).
10, "Hello", truelet, const, or var
let x = 5;
const y = "Hello";
A JavaScript program is made up of statements, which are executed line by line. Each statement performs a specific action.
let a = 5;
let b = 6;
let c = a + b;
Identifiers are names used for variables, functions, and properties. Rules:
$, or _.let or return).myVar is different from myvar).
let myVar = 10;
let $price = 99;
let _name = "John";
Statements are usually terminated with a semicolon. JavaScript has automatic semicolon insertion, but adding them explicitly is recommended for clarity and to prevent subtle errors.
let age = 25;
console.log(age);
Blocks of code are grouped by curly braces { }. They are used in functions, loops, and conditionals to group multiple statements.
if (x > 0) {
console.log("Positive");
}
In the statements example:
let a = 5; stores the value 5 in the variable a.let b = 6; stores the value 6 in the variable b.let c = a + b; adds a and b to get 11 and stores it in c.If you log c using console.log(c);, the console will show 11.
In the code block example, if x is greater than 0, the browser prints "Positive" to the console. Otherwise, the block is skipped.
totalPrice instead of x1).console.log().$, and _).+, -, *, /) in the browser console.if block that checks a variable and logs different messages.