JavaScript syntax refers to the set of rules that define how a JavaScript program is written and interpreted by the browser or JavaScript engine.
1. Variables
Used to store data values.
Declared using var
, let
, or const
.
var x = 10;
let y = 20;
const name = "John";
2. Semicolons
Although semicolons are often optional, it is recommended to use them to terminate statements to avoid potential issues caused by JavaScript’s automatic semicolon insertion.
let x = 5;
let y = 10;
3. Automatic Type Conversion (Coercion)
JavaScript automatically converts data types when performing operations between different types (e.g., adding a number to a string).
let result = 5 + "5"; // "55" (number 5 is converted to string)
4. Identifiers
Identifiers are names given to variables, functions, and objects. They must start with a letter, underscore (_
), or dollar sign ($
), followed by letters, digits, underscores, or dollar signs.
let name = "John";
let $numValue = 100;
let _isUserAllowed = true;
we can’t declare a variable that starts with a number.
let 1name = "John"; // not allowd
5. Reassigning Variables
Variables declared with var or let can be reassigned.
Variables declared with const cannot be reassigned.
let a = 10;
a = 20; // Valid
const b = 50;
// b = 100; // Error: Assignment to constant variable.