Open In App

Variables in TypeScript

Last Updated : 21 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Variables in TypeScript are used to store data values, acting as named memory locations that can hold numbers, strings, booleans, or other types of data.

  • Variables can be declared using let, const, or var depending on the use case.
  • They provide type safety by allowing you to define specific data types like string, number, or boolean.
  • TypeScript supports both explicitly typed and type-inferred variable declarations.

Variable Declaration in TypeScript

In TypeScript, variables are used to store data values, with modern declarations offering strong typing and better scoping rules. Let’s explore how variables are declared in TypeScript.

1. Declare Type and Value in a Single Statement

typescript
let name: string = 'Amit';
const age: number = 25;
  • Type and value are defined together.
  • name is a variable of type string.
  • age is a constant of type number.

2. Declare Type Without Value

typescript
let city: string;
console.log(city); // Output: undefined
  • Only the type is defined; the value is undefined by default.

3. Declare Value Without Type

JavaScript
let country = 'India';
console.log(country); // Output: India
  • The type is any, and the value is initialized to undefined.

Keywords for Variable Declaration

1. var

Traditionally used in JavaScript, var is function-scoped and can lead to unexpected behavior due to hoisting.

JavaScript
var globalVar: number = 10;
console.log(globalVar); // Output: 10
  • Accessible throughout the function in which it’s declared.
  • Prone to issues like variable hoisting and unintended redeclarations.

2. let

Introduced to provide block-level scoping, let confines the variable’s scope to the block in which it’s defined.

JavaScript
let blockScoped: string = 'TypeScript';
console.log(blockScoped); // Output: TypeScript
  • Prevents redeclaration within the same scope.
  • Reduces errors related to variable hoisting.

3. const

Similar to let in terms of block scoping, const is used for variables that should not be reassigned after their initial assignment.

JavaScript
const PI: number = 3.14;
console.log(PI); // Output: 3.14
  • Ensures the variable’s value remains constant.
  • Attempting to reassign will result in a compile-time error.

Note:

  • Variable names can contains alphabets both Upper-case as well as Lower-case and digits also.
  • Variable names can’t start with a digit.
  • We can use _ and $ special characters only, apart from these other special characters are not allowed.

Type Annotations in TypeScript

TypeScript allows explicit type definitions, enhancing code clarity and type safety.

JavaScript
let employeeName: string = 'John Doe';
let employeeAge: number = 30;
const company: string = 'TechCorp';
  • Explicit types help catch errors during development.
  • Improves code readability and maintainability.

Variable Scopes in TypeScript

1. Local Scope

Variables declared within a function or block are accessible only within that function or block.

JavaScript
function example() {
    let localVar: number = 42;
    console.log(localVar); // Output: 42
}
  • Prevents unintended access from outside the block.

2. Global Scope

Variables declared outside any function or block are accessible throughout the entire program.

JavaScript
let globalVar: string = 'Accessible everywhere';
console.log(globalVar); // Output: Accessible everywhere
  • Useful for values that need to be accessed across multiple functions or modules.

3. Class Scope

Variables declared within a class are accessible to all members (methods) of that class.

JavaScript
class Employee {
    salary: number = 50000;
    printSalary(): void {
        console.log(`Salary: ${this.salary}`);
    }
}

const emp = new Employee();
emp.printSalary(); // Output: Salary: 50000

Example of Variables in TypeScript

JavaScript
let globalVar: number = 10;

class Geeks {
    private classVar: number = 11;

    assignNum(): void {
        let localVar: number = 12;
        console.log('Local Variable: ' + localVar);
    }
}

console.log('Global Variable: ' + globalVar);

let obj = new Geeks();
obj.assignNum(); 
  • globalVar is a global variable, accessible throughout the program.
  • classVar is a private class-level variable, accessible only within the Geeks class.
  • localVar is a local variable, accessible only within the assignNum method.

Output:

Global Variable: 10
Local Variable: 12


Next Article

Similar Reads