Member-only story
Source:- How to declare a variable in TypeScript
For more questions and answers visit our website at Frontend Interview Questions
In TypeScript, you can declare a variable using the `let` or `const` keyword, just like in JavaScript. However, TypeScript also allows you to specify the variable type using type annotations.
Here are a few examples:
// Declaring a variable with a type annotation
let myNumber: number = 42;
// Declaring a variable without a type annotation
let myString = "Hello, world!";
// Declaring a constant with a type annotation
const PI: number = 3.14;
// Declaring a variable with an array type
let myArray: string[] = ["apple", "banana", "orange"];
// Declaring a variable with an object type
let myObject: { name: string, age: number } = { name: "Alice", age: 30 };
In the first example, we declare a variable `myNumber` of type `number` and assign it the value `42`. In the second example, we declare a variable `myString` and TypeScript infers its type to be `string` based on the value `”Hello, world!”`.
In the third example, we declare a constant `PI` of type `number` and assign it the value `3.14`.
In the fourth example, we declare a variable `myArray` of type `string[]`, which is an array of strings. We…