Identifiers and Literals
Identifier and Literal are important concepts in programming languages, used to name variables, constants, functions, etc., or to represent the values themselves.
Identifier
- Definition: An identifier is a name used to distinguish variables, functions, objects, etc.
- Example: In
let appleCount = 5;appleCountis the identifier. - Rules:
- An identifier can start with an alphabet, dollar sign (
$), or underscore (_). - From the second character, an identifier can also include numbers.
- Reserved keywords cannot be used as identifiers (e.g.,
let,if, etc.).
- An identifier can start with an alphabet, dollar sign (
Literal
- Definition: A literal is a fixed value directly written in the source code. Literals are used as they are when assigned to a variable or used in operations.
- Example: In
let appleCount = 5;5is a numeric literal. - Types:
- Numeric literals: e.g.,
123,3.14 - String literals: e.g.,
"hello",'world' - Boolean literals: e.g.,
true,false - Object literals: e.g.,
{name: "John", age: 30} - Array literals: e.g.,
[1, 2, 3] - Regular expression literals: e.g.,
/ab+c/
- Numeric literals: e.g.,
In short, an identifier is a name used to refer to or call upon data or functions, while a literal is a fixed value used directly in the code.
Want to learn more?
Join CodeFriends Plus membership or enroll in a course to start your journey.