본문으로 건너뛰기

Identifiers and Literals

Identifiers and Literals

Identifier and Literal are important concepts in programming languages, used to name variables, constants, functions, etc., or to represent the value of the data itself.


Identifier

  • Definition: An identifier is a name used to distinguish variables, functions, objects, etc. For example, a variable name is a type of identifier.
  • Example: In let appleCount = 5;, appleCount is an identifier.
  • Rules:
    • Identifiers can start with a letter, dollar sign ($), or an underscore (_).
    • From the second character onwards, identifiers can include numbers.
    • Reserved words cannot be used as identifiers (e.g., let, if, etc.)

Literal

  • Definition: A literal is a fixed value that appears directly in the source code. Literals are used as written when assigned to variables or used in operations.
  • Example: In let appleCount = 5;, 5 is 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/

In simple terms, an identifier is a name used to refer to or call data or functions, and a literal is a fixed value used directly in the code.