Skip to the workspace
Pebellslearn by building
All about Variables  ›  Lesson

Naming Rules

Variable Naming Rules 12 min

javascript / Naming Rules

Explore each scene at your own pace.

01 · Naming Rules

Naming Rules

Naming Rules

An identifier is a name used in code. JavaScript identifiers are case-sensitive and must follow its naming syntax.

A useful name tells you what a value means, rather than making you remember a code letter.

Make the reading-session panel easier to understand. Rename its bindings, repair a broken name, and watch how the exact spelling connects your code to the result.

Opening your JavaScript project…

Reference and extra examples

Open this whenever you want a fuller explanation. Your project above is the main activity.

Reference model

These authored snapshots explain the example below; they are not a trace of your edited project.

Walk through the modelNaming RulesAuthored snapshots of the example; use Run below to test the code.
readingMinutes4
ReadingMinutes9
Case distinguishes these two bindings.
1 of 3

Follow one example

javascript

The capital R creates a different identifier, so both declarations are valid and hold different values. Names cannot contain a hyphen as part of the identifier: reading-minutes is parsed as subtraction. Start ordinary names with a letter, _ or $, and use later digits where useful. Reserved keywords cannot serve as ordinary variable names.

Predict, change and check

Predict: Would changing the first console.log to readingminutes refer to either declaration?

Test your explanation: Change just the capitalization in one use. Observe the ReferenceError, then repair it. Rename readingMinutes consistently and confirm behavior stays the same.

The tasks below ask you to explain that result and apply the idea to different inputs.

Apply and distinguish

Names should communicate a contract

A legal identifier is not necessarily a useful name. Prefer articleCount for a count, isSaved for a boolean predicate, and loadArticles for an operation. Names are case-sensitive. Hyphens belong in some string IDs but are parsed as subtraction in an unquoted expression, so article-count is not one identifier.

Keep reserved words out of binding names and avoid shadowing globals such as undefined, Number or Object. An object can have a property name that is inconvenient as an identifier: read record['reading-minutes'] with bracket notation. A variable holding a property name is also read through brackets, rather than by writing record.key and expecting the value of key to be substituted.

Check your understanding: State the input contract, identify the operation that changes the result, and give one boundary input that would distinguish the correct rule from the mistake described above.

Scene 1 of 2
Project context and further resources
Additional resources
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.