Identifiers in Dart

In Dart, identifiers are used to name variables, functions, classes, methods, parameters, and other components in your code. These names can include letters, digits, underscores (_), and dollar signs ($), but they must adhere to specific rules for valid naming

📅 Tue Jan 14 20252 min
image

Identifiers can consist of :

* Uppercase letters (A-Z)
* Lowercase letters (a-z)
* Digits (0-9), but cannot start with a digit
* The underscore character (_)
* The dollar sign ($)

Identifiers must follow these rules :

1. First character: The first character of an identifier must be a letter (either uppercase or lowercase) or an underscore (_).
    * Incorrect: 1stName, 9thRoll
    * Correct: _firstName, name1
2. Cannot start with a digit: Identifiers cannot start with a number.
    * Incorrect: 123abc, 9students
    * Correct: abc123, students9
3. No special characters except _ and $:
    * Incorrect: @name, name!
    * Correct: name_1, price$
4. No successive underscores (__): You cannot use consecutive underscores.
    * Incorrect: my__variable
    * Correct: my_variable
5. Unique identifiers: Each identifier in a program must be unique. You cannot use the same name for more than one variable, function, or class in the same scope.
6. Case sensitive: Dart is case-sensitive, which means name, Name, and NAME are three different identifiers.
    * Incorrect: Name = name (as they are different)
    * Correct: Name is different from name.
7. No whitespace allowed: Identifiers cannot have spaces.
    * Incorrect: my name, total amount
    * Correct: myName, totalAmount

Example :

Let's break down a basic Dart program to explain identifiers.

void main() {
  var name = "DevEraa"; // 'name' is an identifier
  var roll_no = 24;   // 'roll_no' is an identifier
  
  print("My name is ${name}. My roll number is ${roll_no}.");
}