How to use identifiers and keywords in TypeScript?

Identifiers

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.

Some rules in naming identifiers in TypeScript

  1. Identifiers are case sensitive. For example, _varname is not the same as _Varname.

  2. Only symbols like and _ can be placed at the beginning of an identifier.

  3. Identifiers can’t start with numbers, only letters.

  4. Identifiers can’t have the same name as keywords.

  5. Spaces cannot be included in an identifier.

Examples of invalid identifiers in TypeScript

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

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.

Examples of keywords in TypeScript

  • break
  • return
  • extends
  • null
  • if
  • continue

Free Resources