AI Reading
Quick summary of this article
TypeScript identifiers are names given to variables and functions, and they must follow specific rules like not starting with a digit and avoiding reserved keywords. Keywords are reserved words with special meanings that cannot be used as identifiers. Comments help make code more readable and are available in two forms: single-line and multi-line.
- Identifiers can include letters, digits, underscores, and dollar signs, but cannot start with a digit or contain spaces or special symbols.
- Identifiers are case-sensitive, meaning
firstNameandFirstNameare treated as different names. - Keywords like
var,let,class, andfunctionare reserved and cannot be used as identifiers. - Single-line comments use
//and multi-line comments use/* ... */to explain code logic. - Well-commented code improves readability and helps you remember the purpose of your code when you revisit it later.
🏷 Identifiers in TypeScript
In TypeScript, we use variables, functions, and other entities by giving them specific names. However, naming these entities follows certain rules and conventions. These rules are referred to as identifiers.
📜 Rules for Identifiers:
- Can contain: Characters (letters), digits (numbers), and underscores (
_), or a dollar sign ($). - Cannot start with: A digit (e.g.,
1numberis invalid). - Cannot include: Special symbols, except for an underscore (
_) or a dollar sign ($). - Cannot be a keyword: Reserved words like
var,let,class, etc., cannot be used as identifiers. - Should not be duplicated: Avoid naming multiple variables or functions with the same name.
- Are case-sensitive:
firstNameandFirstNameare two different identifiers. - Cannot contain spaces: Spaces are not allowed in identifiers.
✅ Example: Valid and Invalid Identifiers
| ✅ Valid Identifiers | ❌ Invalid Identifiers |
|---|---|
firstName |
Var |
first_name |
first name |
num1 |
first-name |
$result |
1number |
🔑 TypeScript Keywords
Keywords are reserved words that have a special meaning in the TypeScript language. These keywords are used to define specific actions or operations and cannot be used as identifiers.
🚨 List of TypeScript Keywords:
break,as,any,switch,caseif,else,throwvar,number,stringget,module,type,instanceoftypeof,public,privateenum,export,finallyfor,while,void,nullsuper,this,new,inreturn,true,false,extendsstatic,let,package,implementsinterface,function,try,yieldconst,continue,do,catch
💬 Comments in TypeScript
To enhance the readability of your code and make it easier to understand, comments are used. Even if you’re a great coder, it’s common to forget why you wrote a specific piece of code after some time. Well-commented code helps a lot in understanding your logic when revisited later.
✍ Types of Comments in TypeScript:
- Single-line Comments
Use//to write comments on a single line.Example:// This is a single-line comment - Multi-line Comments
Use/*and*/to write comments spanning multiple lines.Example:/* This is a Multi-line comment */
