-
1elephant
: not valid.
-
tallGiraffe_1
: valid.
-
$crocodile
: valid.
-
nice-athletic-camel
: not valid.
-
wise_small_hippo
: valid.
-
zebra2
: valid.
-
new
: not valid.
This exercise tests our knowledge of the rules for naming identifiers in JavaScript.
Identifiers
Custom names in a program are usually referred as identifiers. In JavaScript, the identifiers are:
-
Variable names (including function parameters).
-
Function names (including Class names).
-
Property names of objects.
Valid names for identifiers
In JavaScript, the rules for naming an identifier are the following:
-
The name of the identifier usually must not be a reserved JavaScript keyword.
But even if it can be (for specific keywords only, and in certain circumstances only), the best practice is to avoid all reserved keywords.
You can see the full list of the reserved keywords below, in the last section of this document.
-
The first character of the identifier name must be a letter, an underscore (
_
) or a dollar sign ($
).
-
The (optional) following characters can be letters, digits, underscores (
_
) or dollar signs ($
).
Identifier names and numbers
An identifier name cannot start with a digit so that JavaScript can distinguish identifier names from numbers.
Recommended letters: ASCII letters
A letter is usually an ASCII letter, i.e. a non-accentuated letter from the latin alphabet.
The recommended practice is to only use ASCII letters for identifier names in your code.
However, letters from other alphabets are also allowed.
That is why the following code, as weird as it looks, does not throw any error:
const àèìòù = 1 // non-ASCII accentuated letters
const αβΔ = 2; // non-ASCII letters from the greek alphabet
console.log(àèìòù);
console.log(αβΔ);
Specificity of property names of objects
For property names, it is possible to replace an invalid property name with the corresponding name property explicitly written as a string.
Let us have a look:
const obj_1 = { my-name: 'chloe'};
Since my-name
is an invalid identifier (the dash character -
is not allowed),
the previous code throws an error (SyntaxError
) if we run it.
const obj_2 = { 'my-name': 'halle'};
Since 'my-name'
is a string literal, the previous code runs smoothly.
But even though this is possible in JavaScript, we recommend you use only valid property names in your code (see next paragraph).
Having valid identifiers as property names let you access them with the dot notation (which is preferred over the bracket notation when it is possible).
Recommended case for identifiers
In JavaScript, the common practice is to use a recommended case for specific identifiers. In details:
Identifier |
Strongly recommended case |
Variables that can be reassigned or mutated |
camelCase (variableWithLetKeyword ) |
Function parameters |
camelCase (variableForFunction ) |
Variables that store constant values |
UPPERCASE (REAL_CONSTANT ) |
Variables that store a collection of methods (modules) |
camelCase (collectionOfMethods ) |
Property names of an object |
camelCase (propertyName ) |
Factory functions |
PascalCase (FactoryFunction ) |
Non-factory functions |
camelCase (ordinaryFunction ) |
I personnaly do not follow the previous recommendations, because camelCase is less readable than snake_case.
Therefore, I only use camelCase for functions.
For variables, function parameters and perperty names, I only use snake_case.
Here are the 43 reserved keywords in JavaScript:
await
(Reserved in module code).
break
.
case
.
catch
.
class
.
const
.
continue
.
debugger
.
default
.
delete
.
do
.
else
.
enum
(For a possible use in a future specification).
export
.
extends
.
finally
.
for
.
function
.
if
.
implements
(Reserved in strict mode only).
import
.
in
.
instanceof
.
interface
(Reserved in strict mode only).
let
(Reserved in strict mode only).
new
.
package
(Reserved in strict mode only).
private
(Reserved in strict mode only).
protected
(Reserved in strict mode only).
public
(Reserved in strict mode only).
return
.
static
(Reserved in strict mode only).
super
.
switch
.
this
.
throw
.
try
.
typeof
.
var
.
void
.
while
.
with
.
yield
.