Typescript Identifiers and Keywords

In this Typescript tutorial, we will see what typescript identifiers and keywords are, and how we can use identifiers and keywords in typescript.

So here we will discuss the following topics:

  • What is the identifier in typescript?
  • Examples of valid and invalid identifiers in typescript
  • What is a keyword in typescript?
  • What are the different TypeScript declaration keywords for variables?

If you are new to typescript, check out Typescript hello world program for beginners

What is an identifier in typescript?

Basically, the names that are assigned to the members of any class, such as variables, methods, classes, array names, etc., are known as identifiers in typescript.

There are several guidelines to follow while declaring Identifiers in typescript:

  • Typescript Identifier names may begin with either an uppercase or lowercase letter, but not with a number.
  • No other special symbol may be used; only _ and $ symbols may be used to provide names to Identifiers.
  • Typescript Identifiers are distinct from keywords.
  • Identifiers must not contain spaces and are case-sensitive.

Examples of valid and invalid identifiers in typescript

Here we will see an example of valid and invalid identifiers in typescript.

These are the invalid identifiers in typescript, which we should not use while writing code:

  • any
  • 1$spGuides
  • #spguides
  • sp-guides

These are the valid identifiers in typescript, or we can say valid ways to define identifiers while writing code in typescript.

  • spguides
  • sp_guides
  • $spguides
  • _spguides$

Read TypeScript Enum (With Examples)

What is a keyword in typescript

The words that execute a certain function or those that stand for a particular activity are known as keywords. Here is a list of keywords in typescript:

  • throw
  • else
  • if
  • break
  • var
  • typeof
  • as
  • any
  • switch
  • case
  • module
  • type
  • instance of
  • public
  • private

Read TypeScript Set and Weakset

Different TypeScript declaration keywords for variables

Variable declarations in Typescript resemble those in Javascript. Every keyword has a defined range. So in typescript variable can be declared by the below keywords:

  • var
  • let
  • const

Keyword ‘var’

It is used to declare a variable with the var keyword. For example:

var example:number

Variables with a function scope are called var. It means that if they weren’t created within a function, they are globally scoped or that they are only accessible within the function in which they were created. If I define var inside of a function and then attempt to call it from outside the function, it won’t work.

For example, num1 is a number type that is declared globally. The num2 resides in the scope of the function. The function may access the num 3 even if it is declared inside of an if loop since it is declared using the var keyword in typescript.

Next in the app.ts file write the below code:

var num1: number = 1;

 

function var_keyword() {

  var num2: number = 2;

  if (num1 + num2 == 3) {

    var num3: number = 3;

  }

  else{

    var num3: number = 0;

  }

 

  console.log(num1);

  console.log(num2);

  console.log(num3);

}

var_keyword();

Now run the below command, to compile the app.ts file.

tsc app.ts

After you run the above command the app.js file will be generated, and to run the app.js file , write the below command:

node app.js

You can see the below output in the console:

What is a keyword in typescript
What is a keyword in typescript

Now run the below command to see the output from app.ts file.

ts-node app.ts
What is var keyword in typescript
What is var keyword in typescript

Keyword ‘let’

Block scope applies to the let variables in typescript. The let keyword in typescript allows you to define variables that are limited to the scope of the block statement or expression on which it is used, in contrast to the var keyword, which declares a variable globally to the whole function regardless of block scope.

This is how we can declare the variable ‘let’ in typescript:

let str: string ="sp guides"

For example, The num 1 is globally declared. The num2 has the scope of the function in which it resides. The num3 is declared in an if loop, it can only be accessed within the if loop, and it is block-scoped because it is declared using the let keyword. In the app.ts file write the below code

let num1: number = 1;
  
function let_keyword() {
  let num2: number = 2;
  if (num1 + num2 == 3) {
    let num3: number = 3;
  }
  
  console.log(num1);
  console.log(num2);
  console.log(num3); // Throws error
}
  
let_keyword();

Now to compile the app.ts file write the below command, which will throw an error: Cannot find name ‘num3’.

tsc app.ts
What is let keyword in typescript
What is let keyword in typescript

And if we run the app.js file with the below command, you can see the below required result.

node app.js
 let keyword in typescript
let keyword in typescript

Keyword ‘const’

Const variables have a block scope in typescript. Constants are block-scoped, just as variables defined with the let keyword. A constant’s value cannot be changed by assigning a new value to it, and neither can a variable with the same name be redeclared.

For example- The num 1 is a globally proclaimed number. The num 2 has the scope of the function in which it resides. The num3 variable is declared in an if loop, it can only be accessed within the if loop, and it is block-scoped because it is declared with the const keyword.

The const keyword is similar to let in typescript, but the distinction is that const variables cannot be reassigned, whereas let variables may.

const num1: number = 1;
  
function constKeyword() {
  const num2: number = 2;
  if (num1 + num2 == 3) {
    const num3: number = 3;
  }
  
  console.log(num1);
  console.log(num2);
  console.log(num3); // Throws error
}
  
constKeyword();

Compile the .ts file with the below code, and you can see the below error for num 3.

tsc app.ts
What is const keyword in typescript
What is the const keyword in typescript

If I will run the generated app.js file, then you can see the below output :

node app.js
 const keyword in typescript
const keyword in typescript

Conclusion

In this Typescript tutorial, we saw what are Typescripts and identifiers, and how we can use identifiers and keywords in our code.

  • What is the identifier in typescript?
  • Examples of valid and invalid identifiers in typescript
  • What is a keyword in typescript?
  • What are the different TypeScript declaration keywords for variables?

You may like the following tutorials:

>