JavaScript syntax is the set of rules that defines how JavaScript code must be written. If statements are the instructions, syntax is the structure that makes those instructions valid and understandable to the browser.
Syntax controls how you write variables, values, operators, expressions, and statements. When your syntax is correct, the browser can read your code and run it properly. When your syntax is wrong, JavaScript throws an error and stops the code from working as expected.
When people talk about JavaScript syntax, they are usually talking about the basic building parts of code, such as values, operators, expressions, keywords, and names. Once you understand how these pieces fit together, writing JavaScript becomes much easier and much less confusing.
Values are the pieces of data JavaScript works with. They can be fixed values, such as numbers and strings, or variable values stored under a name.
10
"Hello"
true
In the example above, 10, "Hello", and true are
all valid JavaScript values.
Operators are symbols that tell JavaScript to perform an action. For example,
= assigns a value, while + adds values together.
let x = 5;
let y = 6;
let total = x + y;
Here:
= stores a value in a variable+ adds two values togetherAn expression is any piece of code that produces a value. Expressions are often used inside statements.
5 + 6
"John" + " " + "Doe"
The first expression produces 11. The second produces the string
"John Doe".
Keywords are reserved words that already have a special meaning in JavaScript. You use them to define how your code behaves.
let message = "Welcome";
if (message) {
console.log(message);
}
In this code:
let creates a variableif starts a conditionlet, if, or function are not
allowed as identifiers.
Identifiers are the names you give to variables, functions, and other code elements. JavaScript has a few naming rules:
let userName = "Rahim";
let _total = 100;
let $price = 25;
JavaScript treats uppercase and lowercase letters as different characters. That means variable names must match exactly.
let lastName = "Islam";
let lastname = "Khan";
These are two different variables because lastName and
lastname are not the same.
camelCase for variables. It makes your code easier to read and less likely
to break because of case mistakes.
JavaScript supports Unicode, which means you can use many international characters in strings and text content. In practice, most developers still use simple and readable English-based variable names for better compatibility and teamwork.
| Syntax Part | What It Does |
|---|---|
| Values | Represent data such as numbers, strings, and booleans |
| Operators | Perform actions like assignment and addition |
| Expressions | Produce a value from code |
| Keywords | Reserved words with special meaning in JavaScript |
| Identifiers | Name variables, functions, and other program elements |
| Case Sensitivity | Makes uppercase and lowercase names different |