codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🔷

TypeScript

4 / 60 topics
1Getting Started with TypeScript2Installation of TypeScript3TypeScript Syntax4Variables in TypeScript5Data Types in TypeScript6Arrays in TypeScript7Objects in TypeScript
Tutorials/TypeScript/Variables in TypeScript
🔷TypeScript

Variables in TypeScript

Updated 2026-04-20
3 min read

Variables in TypeScript

Variables are fundamental building blocks in any programming language, including TypeScript. They allow you to store and manipulate data throughout your application. In this section, we will explore the various types of variables available in TypeScript, how to declare them, and best practices for using variables effectively.

Introduction to Variables

A variable is a named storage location that holds a value. In TypeScript, like in JavaScript, variables are used to store information that can be manipulated or referenced throughout your code. TypeScript adds static typing to JavaScript, which means you can specify the type of data a variable will hold, enhancing code safety and readability.

Declaring Variables

In TypeScript, you can declare variables using let, const, or var. Each has its own use case and scope rules.

Using let

The let keyword is used to declare block-scoped variables. This means the variable is only accessible within the block it is declared in (e.g., inside curly braces {}).

function exampleLet() {
  let x = 10;
  if (true) {
    let x = 20; // Different variable, same name
    console.log(x); // Outputs: 20
  }
  console.log(x); // Outputs: 10
}

exampleLet();

Using const

The const keyword is used to declare block-scoped variables that cannot be reassigned. This makes const ideal for constants or values that should not change after initialization.

function exampleConst() {
  const y = 30;
  // y = 40; // Error: Cannot assign to 'y' because it is a constant.
  console.log(y); // Outputs: 30
}

exampleConst();

Using var

The var keyword is used to declare function-scoped variables. This means the variable is accessible within the entire function, regardless of block scope.

function exampleVar() {
  var z = 50;
  if (true) {
    var z = 60; // Same variable, same name
    console.log(z); // Outputs: 60
  }
  console.log(z); // Outputs: 60
}

exampleVar();

Type Annotations

TypeScript allows you to specify the type of a variable explicitly using type annotations. This helps catch errors at compile time and improves code readability.

let age: number = 25;
let name: string = "Alice";
let isStudent: boolean = true;

function greet(person: { name: string; age: number }): void {
  console.log(`Hello, ${person.name}! You are ${person.age} years old.`);
}

greet({ name: "Bob", age: 30 });

Basic Types

TypeScript supports several basic types:

  • number: Represents numeric values (both integers and floating-point numbers).
  • string: Represents textual data.
  • boolean: Represents true or false values.
  • null and undefined: Represent the absence of a value.
  • any: Represents any type, effectively disabling type checking.

Arrays

Arrays can be typed using square brackets [] to specify the type of elements they hold.

let numbers: number[] = [1, 2, 3, 4];
let names: string[] = ["Alice", "Bob", "Charlie"];

Tuples

Tuples are arrays with a fixed length and known types for each element.

let person: [string, number] = ["Alice", 25];

Type Inference

TypeScript can infer the type of a variable based on its initial value. This feature simplifies code by reducing the need for explicit type annotations.

let inferredAge = 30; // TypeScript infers 'number' type
let inferredName = "Bob"; // TypeScript infers 'string' type

However, it's important to note that while type inference is powerful, it can sometimes lead to less clear code if the initial value is not immediately obvious.

Best Practices

  1. Use const by default: Whenever possible, use const to declare variables that should not be reassigned. This makes your code more predictable and easier to maintain.
  2. Be explicit with types: While type inference is convenient, explicitly annotating types can improve code readability and catch potential errors early.
  3. Avoid using var: The function-scoped nature of var can lead to unexpected behavior. Use let or const instead for block-scoped variables.
  4. Use tuples for fixed-length arrays with known types: Tuples provide a way to enforce the structure and types of data in an array, which is useful for functions that return multiple values.

Conclusion

Variables are essential components of any programming language, and TypeScript provides robust tools for managing them. By understanding how to declare variables using let, const, and var, and by leveraging type annotations and inference, you can write safer, more maintainable code. Following best practices will help ensure your TypeScript applications are both efficient and easy to understand.

In the next section, we will explore functions in TypeScript, including how to define them, use parameters, and return values. Stay tuned!


PreviousTypeScript SyntaxNext Data Types in TypeScript

Recommended Gear

TypeScript SyntaxData Types in TypeScript