JavaScript Syntax

The Rules JavaScript Follows

JavaScript Syntax

The grammar of JavaScript — values, operators, expressions, identifiers, and the rules for naming things.

5 min read Level 1/5 #syntax#values#expressions
What you'll learn
  • Distinguish literal values from variable values
  • Read and write expressions
  • Name variables correctly using camelCase
  • Know that JavaScript is case-sensitive

Syntax is the set of rules for writing valid JavaScript. Get a feel for these rules now and everything that follows will click faster.

Values

There are two kinds of values:

  • Literal values — written directly: 42, "hello", true, null, [1, 2, 3].
  • Variable values — a name that refers to a value: price, userName, total.
Literals and variables script.js
console.log(42);           // literal number
console.log("hello");      // literal string
let price = 9.99;          // variable holding a literal
console.log(price);
▶ Preview: console

Operators

Operators do something with values. You’ve already seen = (assignment) and + (addition). Others include -, *, /, %, ===, &&, ||, and more — we’ll cover them in detail later.

A few operators script.js
console.log(2 + 3);      // 5
console.log(10 / 4);     // 2.5
console.log("Js" + "schools"); // "Jsschools"
console.log(5 === 5);    // true
▶ Preview: console

Expressions

An expression is any piece of code that produces a value.

5 + 3           // expression — produces 8
"Hi, " + name   // expression — produces a string
price * 1.07    // expression — produces a number

Statements use expressions: let total = price * 1.07; is a statement containing the expression price * 1.07.

Identifiers (Names)

Identifiers are the names you give to variables, functions, and classes. The rules:

  • Must start with a letter, underscore _, or dollar sign $.
  • After the first character, can contain letters, digits, _, or $.
  • Cannot be a reserved keyword (let, class, return, …).
  • Are case-sensitive — userName and username are different names.
Valid and invalid names script.js
let firstName = "Ada";   // ✅ valid
let _temp = 0;           // ✅ valid (underscore start)
let $el = null;          // ✅ valid (dollar sign start)
let user2 = "Lin";       // ✅ valid (digits allowed after first char)

// let 2user = "no";     // ❌ can't start with a digit
// let user-name = "no"; // ❌ hyphens not allowed
// let class = "no";     // ❌ "class" is a reserved keyword
▶ Preview: console

camelCase Convention

JavaScript names use camelCase by convention: lowercase first word, capital first letter on each word after.

firstName
totalPrice
isUserLoggedIn
getUserById

It’s a convention, not a rule — but every other JavaScript developer follows it, so you should too. Class names are an exception: they use PascalCase (also called UpperCamelCase): UserProfile, HttpClient.

Case Sensitivity

JavaScript is case-sensitive. Two names that differ only in case are two different names.

Case matters script.js
let userName = "Ada";
let username = "Lin";

console.log(userName); // "Ada"
console.log(username); // "Lin"
▶ Preview: console

This is a common source of bugs — typing USERNAME when you meant userName will give you ReferenceError: USERNAME is not defined.

Up Next

Before we dive into variables proper, one quick stop: how to add notes to your code that JavaScript ignores.

JavaScript Comments →