Camel case
currentTempC
,
anIdentifierName
(so named because the capital letters look like
the humps in a camel’s back).
Snake case
current_temp_c
,
an_identifier_name
(slightly less popular).
You can use whichever convention you prefer, but consistency is a good idea: select
one and stick with it. If you are working on a team or making your project available
to a community, try choosing whatever the preferred convention is.
It is also advisable to adhere to the following conventions:
• Identifiers shouldn’t start with a capital letter except for classes (which we’ll cover
in
).
• Very often, identifiers that start with one or two underscores are used to repre‐
sent special or “internal” variables. Unless you need to create your own special
category of variables, avoid starting variable names with an underscore.
• When using jQuery, identifiers that start with a dollar sign conventionally refer
to jQuery-wrapped objects (see
).
Literals
We’ve already seen some literals: when we gave
currentTempC
a value, we provided a
numeric literal (
22
at initialization, and
22.5
in the next example). Likewise, when we
initialized
room1
, we provided a string literal (
"conference_room_a"
). The word lit‐
eral means that you’re providing the value directly in the program. Essentially, a lit‐
eral is a way to create a value; JavaScript takes the literal value you provide and creates
a data value from it.
It’s important to understand the difference between a literal and an identifier. For
example, think back to our earlier example where we created a variable called
room1
,
which had the value
"conference_room_a"
.
room1
is an identifier (referring to a con‐
stant), and
"conference_room_a"
is a string literal (and also the value of
room1
). Java‐
Script is able to distinguish the identifier from the literal by the use of quotation
marks (numbers don’t need any sort of quotation because identifiers can’t start with a
number). Consider the following example:
let
room1
=
"conference_room_a"
;
// "conference_room_a" (in quotes) is
// a literal
let
currentRoom
=
room1
;
// currentRoom now has the same value
// as room1 ("conference_room_a")
36 | Chapter 3: Literals, Variables, Constants, and Data Types