Identifiers simply describe the names we use when referring to instances like variables, functions, etc., that are declared in a program.
Every programming language has specific rules and regulations to follow when declaring identifiers, and TypeScript is no exception.
However, TypeScript observes most of JavaScript’s naming rules.
Identifiers are case sensitive. For example, _varname
is not the same as _Varname
.
Only symbols like $
and _
can be placed at the beginning of an identifier.
Identifiers can’t start with numbers, only letters.
Identifiers can’t have the same name as keywords.
Spaces cannot be included in an identifier.
Invalid Identifier | Reason | Valid form |
var name | spaces not allowed | varname |
Var@name | '@' not allowed | Varname |
2varname | can't start with a digit | $varname |
-varname | '-' not allowed , only '_' or '$' | _varname |
return | return is a reserved keyword |
Keywords are reserved words in a programming language that have special meaning and perform a specific function in a program.
There are reserved in the sense that they can only be used for a purpose specified by the programming language, and they can’t be used as identifiers for variables, functions, etc.
When keywords are mistakenly used for purposes they are not intended for, compile errors are generated.
break
return
extends
null
if
continue