Legal and Illegal Variable Names in TypeScript
Variable naming is an important aspect of writing clean and maintainable code in TypeScript (or any programming language). There are specific rules and conventions you must follow to ensure your variable names are legal. This blog post will outline the guidelines for naming variables in TypeScript, highlighting what constitutes legal and illegal variable names.
Legal Variable Names
Start with a Letter, Underscore (_), or Dollar Sign ($): Variable names must begin with a letter (a-z, A-Z), an underscore (_), or a dollar sign ($).
typescriptlet name = "Alice"; let _age = 25; let $salary = 50000;
Contain Letters, Numbers, Underscores, or Dollar Signs: After the first character, variable names can include letters, numbers (0-9), underscores, and dollar signs.
typescriptlet user1 = "Bob"; let _totalAmount = 150.75; let $price_after_discount = 99.99;
Case Sensitivity: Variable names are case-sensitive, meaning
myVar
andmyvar
are considered different variables.typescriptlet myVar = "Hello"; let myvar = "World"; console.log(myVar); // "Hello" console.log(myvar); // "World"
Descriptive Names: While not a strict rule, using descriptive names for variables improves code readability and maintainability.
typescriptlet firstName = "John"; let numberOfItems = 10;
Illegal Variable Names
Starting with a Number: Variable names cannot begin with a number.
typescriptlet 1stName = "Alice"; // SyntaxError
Using Reserved Keywords: Variable names cannot be reserved words or keywords in TypeScript, such as
let
,const
,class
,function
, etc.typescriptlet let = 10; // SyntaxError let class = "Math"; // SyntaxError
Including Spaces: Variable names cannot contain spaces.
typescriptlet first name = "Alice"; // SyntaxError
Special Characters (Other than $ and _): Variable names cannot contain special characters like
!
,@
,#
,%
,^
,&
,*
,(
,)
,-
,+
,=
,{}
,[]
,;
,:
,,
,<
,>
,/
,?
,\
,|
, etc.typescriptlet user-name = "Bob"; // SyntaxError let user@domain = "alice@example.com"; // SyntaxError
Examples of Legal and Illegal Variable Names
Here are some examples to illustrate legal and illegal variable names:
Legal Variable Names:
typescriptlet userName = "Alice";
let _userAge = 25;
let $userSalary = 50000;
let totalAmount1 = 150.75;
let user_name = "Bob";
Illegal Variable Names:
typescriptlet 1stUser = "Alice"; // Illegal: starts with a number
let function = "login"; // Illegal: uses a reserved keyword
let user name = "Bob"; // Illegal: contains a space
let user-name = "Charlie"; // Illegal: contains a hyphen
Best Practices for Naming Variables
Be Descriptive: Use meaningful and descriptive names that clearly indicate the purpose of the variable.
typescriptlet userName = "Alice"; let numberOfItemsInCart = 10;
Use CamelCase for Multi-Word Variables: Adopt camelCase (first word lowercase, subsequent words capitalized) for variable names.
typescriptlet firstName = "John"; let totalAmountDue = 200.50;
Avoid Abbreviations: Avoid using abbreviations or single-letter names, except in loop counters or temporary variables.
typescriptlet userAge = 30; // Better than "usrAg"
Consistency: Follow a consistent naming convention throughout your codebase to enhance readability and maintainability.
Understanding and following the rules for legal variable names in TypeScript ensures your code is syntactically correct and easier to read. By adopting best practices for naming variables, you can write more maintainable and understandable code.