Identifiers and Literals
Identifiers
and Literals
are important concepts in programming languages, used to name variables, constants, functions, and to represent fixed values in the code.
Identifier
- Definition: An identifier is a name used to identify variables, functions, objects, etc.
- Example: In
let appleCount = 5;
,appleCount
is an identifier. - Rules:
- Identifiers can start with a letter, a dollar sign (
$
), or an underscore (_
). - Subsequent characters can also be digits.
- Reserved words cannot be used as identifiers. (e.g.,
let
,if
, etc.)
- Identifiers can start with a letter, a dollar sign (
Literal
- Definition: A literal is a fixed value that appears directly in the source code. It is used exactly as it is when assigned to a variable or used in an operation.
- Example: In
let appleCount = 5;
,5
is a numeric literal. - Types:
- Numeric literal: e.g.,
123
,3.14
- String literal: e.g.,
"hello"
,'world'
- Boolean literal: e.g.,
true
,false
- Object literal: e.g.,
{name: "John", age: 30}
- Array literal: e.g.,
[1, 2, 3]
- Regular expression literal: e.g.,
/ab+c/
- Numeric literal: e.g.,
In short, an identifier is a name used to reference or call data or functions, while a literal is a fixed value directly used in the code.
Mission
0 / 1
An identifier is a name used to distinguish variables, functions, objects, etc.
True
False
Lecture
AI Tutor
Design
Upload
Notes
Favorites
Help
Code Editor
Run
Execution Result