TExcelTAD
Home › JavaScript › JS Syntax

JavaScript Syntax

Syntax is the set of rules that defines how a JavaScript program is written: how values, variables, operators and expressions fit together. Code that breaks these rules doesn't run at all — the engine reports a SyntaxError before executing a single line.

Values: literals and variables

JavaScript works with two kinds of values:

  • Literals — fixed values written directly in the code: 42, 3.14, "hello", 'world', true, [1, 2], { x: 1 }.
  • Variables — named containers whose value can be stored and changed.
js · try it yourself
console.log(1999);          // number literal
console.log(10.75);         // decimals use a dot
console.log("double");      // string in double quotes
console.log('single');      // ...or single quotes
console.log(`backticks`);   // ...or backticks (template literal)

let year = 2025;            // a variable holding a number
console.log(year);
1999
10.75
double
single
backticks
2025

Operators and expressions

Operators compute values: + - * / for arithmetic, = to assign. An expression is any combination of values, variables and operators that produces a value:

js · try it yourself
let price = 12;
let quantity = 3;
let total = price * quantity;   // the expression price * quantity evaluates to 36
console.log(total);
console.log(5 + 6 * 2);         // multiplication first
console.log("Java" + "Script"); // + also joins strings
36
17
JavaScript

Identifiers

An identifier is a name you choose for a variable, function or class. The rules:

  • It can contain letters, digits, _ and $.
  • It must not start with a digit.
  • It cannot be a reserved keyword such as let, if or class.
Valid Invalid Why invalid
userName 2ndPlace starts with a digit
_count my-name - is the minus operator
$button class reserved keyword
total2 first name contains a space

Case sensitivity

JavaScript is case-sensitive: total, Total and TOTAL are three different names.

js · try it yourself
let color = "red";
let Color = "blue";
console.log(color, Color);
red blue

The same applies to keywords and built-ins: Let is not a keyword, and Console.log doesn't exist.

Naming conventions

JavaScript developers write most names in camelCase: the first word lowercase, each following word capitalised.

Convention Used for Example
camelCase variables, functions firstName, getTotal()
PascalCase classes, constructors ShoppingCart
UPPER_SNAKE_CASE fixed configuration constants MAX_ITEMS

Syntax errors

If the code can't be parsed, nothing runs. Here a closing parenthesis is missing, so even the first line is never printed:

js
console.log("This line never runs");
console.log("Oops";
// SyntaxError: missing ) after argument list

In a playground, the error message tells you what the parser expected. Read it carefully — it usually points right at the problem.

Tip: A good editor (VS Code, for example) underlines syntax errors as you type, long before you run the code.

Test yourself

Which of these is a valid identifier?

  1. 3d
  2. user-id
  3. $total
  4. new

Are score and Score the same variable?

  1. Yes
  2. No, JavaScript is case-sensitive
  3. Only in strict mode
  4. Only inside functions

What does 5 + 6 * 2 evaluate to?

  1. 22
  2. 17
  3. 13
  4. 30