TypeScript Variables

TypeScript Variables

TypeScript is a statically typed language that allows developers to define variables with a specific type. In this lesson, we will learn how to declare variables in TypeScript and explore the different types available.

Declaring Variables in TypeScript

To declare a variable in TypeScript, we use the let or const keyword, followed by the variable name and an optional type annotation:

let message: string = "Hello, TypeScript!";
const pi: number = 3.14;

The let keyword is used for variables that can be reassigned, while the const keyword is used for variables that are read-only and cannot be reassigned.

Types of Variables in TypeScript

TypeScript provides several types of variables, including:

  • number: A numeric value, including integers and floating-point numbers.
  • string: A sequence of characters that represent text.
  • boolean: A value that can be either true or false.
  • array: A collection of values of the same type.
  • tuple: An array with a fixed number of elements, where each element may have a different type.
  • enum: A way to define a set of named constants.
  • any: A type that represents any value.
  • void: A type that indicates the absence of a value.

Examples

let age: number = 30;
let name: string = "John Doe";
let isStudent: boolean = true;
let grades: number[] = [90, 85, 95];
let contact: [string, number] = ["John Doe", 1234567890];
enum Color {Red, Green, Blue};
let color: Color = Color.Red;
let anyValue: any = "Hello World!";
let nothing: void = undefined;

FAQs

Q: Can I declare a variable without a type annotation in TypeScript?

A: Yes, you can declare a variable without a type annotation, and TypeScript will automatically infer the type based on the value assigned to the variable.

Q: Can I declare a variable with a different type than the one initially assigned to it?

A: No, once you declare a variable with a specific type, you cannot assign a value of a different type to it.

Q: Can I declare a variable as undefined in TypeScript?

A: Yes, you can declare a variable as undefined by assigning it the value undefined or by omitting the value assignment. However, it is recommended to use the null type instead of undefined to indicate the absence of a value.

Q: What is the difference between null and undefined in TypeScript?

A: null and undefined are both used to represent the absence of a value, but null is typically used to represent a deliberate absence of a value, while undefined is used to represent an unintentional absence of a value.

Points to remember:

  • TypeScript variables can be defined using let, const, and var keywords
  • let and const variables are block-scoped, while var variables are function-scoped
  • Type annotations can be added to variables for type safety
  • TypeScript allows optional and default parameter values in functions

Conclusion:

TypeScript provides a strong typing system for variables, which can help prevent runtime errors and make code more predictable. By using let and const keywords, variables can be scoped to specific blocks, making code easier to reason about. Optional and default parameter values in functions can also make code more flexible and easier to use.