Table of contents
1.
Introduction
2.
TypeScript variables
2.1.
Syntax
3.
Naming conventions in typeScript
4.
Types of variables
5.
Variable Scope
5.1.
Global Scope
5.2.
Class Scope
5.3.
Local Scope
5.4.
Output
6.
FAQs
7.
Key Takeaways
Last Updated: Mar 27, 2024

Variables in TypeScript

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

A variable is a storage location of any value to be used or referenced by programs. In other words, a variable stores the values that we need throughout the program and allows us to use them in any context when necessary. Let’s explore how to use variables in TypeScript in this article.

TypeScript variables

TypeScript is an open-source, object-oriented programming language. It is a superset of javascript and a typed language. It compiles JavaScript as it cannot run directly on the browser. The files are saved with a .ts extension in typescript.

The variables are declared before we use them in typescript. The variables are declared using the var keyword before the variable name and a type annotation (: type) after the variable (type annotation is not mandatory). You can learn more about type annotation from this article. 

Syntax

var variableName: type = value;

Let us get a better understanding with an example.

var name: string = “coding ninjas”; // variable with type annotation and value
var name: string; // variable with only type annotation
var name = “coding ninjas”; // variable with value
var name; // variable


In the above code, 

  1. The variable name in the first line has a type annotation as a string, and a value coding ninjas is assigned to it.
  2. The variable name in the second line has only the type annotation and no value, so initially, it will be set to undefined. Later it can be assigned to the program.
  3. The variable name in the third line has a value coding ninjas assigned to it. As we did not mention any type, it will be considered any or inferred to the value type mentioned sometimes.
  4. The variable name is only declared in the fourth line and has no type or value, so it will be set to type any and initialized with undefined.

A variable in typescript can be declared in any of the four ways mentioned above, and all the compilers will accept them.

Naming conventions in typeScript

A developer must follow a few naming conventions to declare the typescript variables. They are:

  • The variable must contain an alphabet or digits.
  • The variable cannot start with digits.
  • The variable cannot contain special characters other than underscore(_) and dollar($).

Types of variables

The variables in typescript can also be declared using let and const according to their scope and necessity in code.

let:

The keyword let is similar to var. The let keyword has scope restrictions in typescript. The variables declared with let can only be used within their block scope and cannot be redeclared within the same block. The variables with the same name can be declared in different blocks.

let age: number = 20;


Redeclaring any let variable will display an error like:

Compiler Error: Cannot redeclared block-scoped variable ‘age’.

const:

The keyword const prevents the compiler from re-assigning any value to the variable.

const name = “coding ninjas”;


Re-assigning a value to a const variable will display an error like:

Compiler Error: Cannot assign to 'name' because it is a constant or read-only property

Variable Scope

The variables have a scope in typescript. The scope is a block or area where the variable declared will be identified and used. The availability of a variable can be determined by its scope. The types of scopes are:

Global Scope

The variables with the global scope are declared outside the programming constructs and can be accessed anywhere from the code.

Class Scope

The variables with the class scope are also called fields. They are declared inside a class nut outside the methods and blocks. They can be used anywhere within the class.

Local Scope

The variables with local scope belong to the methods or blocks. They can be used and accessed only inside the block they are declared.

var num = 20; // global variable
class Example {
    var value = 10; // class or instance variable
    console.log(“value in class: ” +value);
    function() {
        let value = 5; // local variable
        console.log(“value in function: ” +value);
    }
}

Output

value in class: 10

value in function: 5

In the above code, the function considers the local values first and prints them, and similarly, the class considers the class variable. If no local variable is available, it checks outside the block for a value. 

FAQs

  1. How do I declare variables in TypeScript?
    The variables can be declared in typescript using var keyword before the variable name and a type annotation (: type) after the variable (type annotation is not mandatory).
     
  2. What is the difference between var and let in TypeScript?
    The keyword var is function scoped, whereas the keyword let is block scoped. The variable declared with var has more scope than let.
     
  3. What are variable scopes available in TypeScript?
    The scopes available in typescript are; global, class, and local.

Key Takeaways

Let’s look at what we discussed in this article, in brief,

  • A variable is a storage location of any value to be used or referenced by programs. It stores the values that we need throughout the program and allows us to use them in any context when necessary.
  • The variables are declared before we use them in typescript. The variables are declared using the var keyword before the variable name and a type annotation (: type) after the variable.
  • Every variable must be declared by following the naming conventions in typescript.
  • The variables in typescript can also be declared using varlet, and const according to their scope and necessity in code.
  • The variables have a scope in typescript. The availability of a variable can be determined by its scope. The scopes available in typescript are; global, class, and local.
     

Recommended Readings:

Hey ninjas! If you like typescript and want to explore it, we have the best web development course for you. Check it out and share things you learned on social media by tagging coding ninjas.
 

Happy Learning!

Live masterclass