Skip to content

Identifiers & IdentifierNames

The JavaScript specification uses two terms for names. An IdentifierName is any name that follows the character rules, and an Identifier is an IdentifierName that is not a reserved word. Variables, functions, classes and parameters need an Identifier, while property names accept any IdentifierName, which is why lesson.class is legal but let class is not.

A name starts with a letter, $ or _, and can go on with letters, digits, $ and _. Letters here means Unicode letters, so names in other alphabets are allowed. A name cannot start with a digit, and it is case-sensitive, so total and Total are different names.

const $total = 42.5;
const _count = 3;
const item2 = 'pencil';
const café = 'open';
const π = 3.14159;
console.log($total, _count, item2, café, π);

This prints:

42.5 3 pencil open 3.14159

Non-ASCII names work, but they are harder to type and to search for, so most codebases keep to ASCII letters in camelCase.

A reserved word cannot be used as an Identifier. Trying to declare one is a syntax error, which stops the whole script before any of it runs.

let class = 'Year 9';

Node reports:

SyntaxError: Unexpected token 'class'

The reserved words in current JavaScript are:

  • Always reserved: break, case, catch, class, const, continue, debugger, default, delete, do, else, enum, export, extends, false, finally, for, function, if, import, in, instanceof, new, null, return, super, switch, this, throw, true, try, typeof, var, void, while and with.
  • Reserved in strict mode: implements, interface, let, package, private, protected, public, static and yield. Strict mode also forbids arguments and eval as variable names.
  • Reserved in modules and async functions: await. yield is likewise reserved inside generator functions.

Words such as abstract, boolean, int, goto and final were reserved in the earliest versions of the language for possible future use, but ES5 released them and they are ordinary names today. Words with a special meaning in only one place, such as async, of, get, set, from and as, are not reserved at all.

Outside strict mode, the strict-mode words are still valid variable names, because older scripts used them before they were reserved. Browsers’ classic scripts behave the same way as this CommonJS file.

var let = 'allowed in sloppy mode';
var yield = 'also allowed';
console.log(let, yield);

This prints:

allowed in sloppy mode also allowed

With 'use strict'; at the top of the file, the same var let line fails:

SyntaxError: Unexpected strict mode reserved word

Using these words as names is legal in sloppy code but confusing, so avoid it. See Strict Mode for what else strict mode changes.

Property names follow the IdentifierName rule, so reserved words are fine both in an object literal and after a dot.

const lesson = {
class: 'Year 9',
for: 'revision',
new: true,
};
console.log(lesson.class, lesson.for, lesson.new);

This prints:

Year 9 revision true

A property name can also be any string at all, written in quotes, or a number, or a computed key in square brackets. A name that is not a valid IdentifierName has to be read with bracket notation. Numeric keys are stored as strings, and integer-like keys come first when the keys are listed.

const field = 'colour';
const product = {
'unit price': 4.99,
2: 'second shelf',
[field]: 'green',
};
console.log(product['unit price']);
console.log(product[2], product['2']);
console.log(product.colour);
console.log(Object.keys(product));

This prints:

4.99
second shelf second shelf
green
[ '2', 'unit price', 'colour' ]

A computed key can also be a symbol, which is the only kind of property key that is not a string.

Steve Griffith covers this topic in his video Variable and Property Names in JS.