TypeScript Variables and Functions

the illustrations of different types of tools are all painted on the boarder board of this device

Note: this page has been created with the use of AI. Please take caution, and note that the content of this page does not necessarily reflect the opinion of Cratecode.

TypeScript, a superset of JavaScript, adds optional static typing to the language. This makes it easier to catch bugs and write more maintainable code. In this guide, we'll explore how to create and use variables and functions in TypeScript.

Variables

Variables in TypeScript are used to store values, and their types are inferred by the compiler. You can declare variables using the let and const keywords.

let

The let keyword is used to declare a variable that can be reassigned later. Here's an example:

let greeting = "Hello, TypeScript!"; console.log(greeting); // Output: Hello, TypeScript!

const

The const keyword is used to declare a constant variable, which means its value cannot be changed after it's been assigned. Here's an example:

const pi = 3.14159; console.log(pi); // Output: 3.14159

Type Annotation

TypeScript allows you to explicitly specify the variable's type using a type annotation. Here's an example:

let age: number = 25;

In this example, age has a type annotation number, which means the value of age must be a number.

Functions

Functions in TypeScript are used to perform a specific task, and you can define them using the function keyword. Functions can accept parameters, and you can also specify the return type.

Basic Function

Here's a simple TypeScript function that adds two numbers:

function add(a: number, b: number): number { return a + b; } console.log(add(5, 3)); // Output: 8

In this example, the function add takes two parameters, a and b, both of type number. The function returns a number as well.

Optional Parameters

TypeScript allows you to specify optional parameters in functions using the ? symbol. Optional parameters must be defined after required parameters. Here's an example:

function greet(name: string, age?: number): string { if (age) { return `Hello, ${name}! You are ${age} years old.`; } else { return `Hello, ${name}!`; } } console.log(greet("Alice")); // Output: Hello, Alice! console.log(greet("Bob", 30)); // Output: Hello, Bob! You are 30 years old.

In this example, the age parameter is optional. If it's not provided, the function simply returns "Hello, [name]!".

Default Parameters

You can also set default values for function parameters in TypeScript. If the parameter is not provided, the default value will be used. Here's an example:

function multiply(a: number, b: number = 2): number { return a * b; } console.log(multiply(5)); // Output: 10

In this example, the b parameter has a default value of 2. If it's not provided, the function will multiply a by 2.

Now that you've learned the basics of variables and functions in TypeScript, you're on your way to becoming a more proficient programmer. Keep exploring and practicing to build on this foundation!

Hey there! Want to learn more? Cratecode is an online learning platform that lets you forge your own path. Click here to check out a lesson: Advanced Data Types (psst, it's free!).

Similar Articles