TypeScript Fundamentals: Variables, Types, and Functions
Are you ready to take your programming skills to the next level? If so, then TypeScript is the language for you! TypeScript is a superset of JavaScript that adds static typing, classes, and interfaces to the language. This makes it easier to write and maintain large-scale applications.
In this article, we will cover the fundamentals of TypeScript, including variables, types, and functions. By the end of this article, you will have a solid understanding of these concepts and be ready to start writing TypeScript code.
Variables
Variables are used to store data in a program. In TypeScript, variables can be declared using the let
or const
keywords. The let
keyword is used to declare variables that can be reassigned, while the const
keyword is used to declare variables that cannot be reassigned.
let name: string = "John";
const age: number = 30;
In the example above, we declare two variables: name
and age
. The name
variable is of type string
, and the age
variable is of type number
. We also assign values to these variables using the =
operator.
TypeScript also supports type inference, which means that the type of a variable can be inferred from its value. For example, if we declare a variable and assign it a string value, TypeScript will infer that the variable is of type string
.
let message = "Hello, TypeScript!";
In the example above, TypeScript infers that the message
variable is of type string
.
Types
Types are used to define the shape of data in a program. TypeScript supports several built-in types, including string
, number
, boolean
, object
, and any
.
let name: string = "John";
let age: number = 30;
let isStudent: boolean = true;
let person: object = { name: "John", age: 30 };
let anything: any = "Hello, TypeScript!";
In the example above, we declare variables of different types. The name
variable is of type string
, the age
variable is of type number
, the isStudent
variable is of type boolean
, the person
variable is of type object
, and the anything
variable is of type any
.
TypeScript also supports arrays and tuples. An array is a collection of values of the same type, while a tuple is a collection of values of different types.
let numbers: number[] = [1, 2, 3, 4, 5];
let person: [string, number] = ["John", 30];
In the example above, we declare an array of numbers and a tuple that contains a string and a number.
Functions
Functions are used to perform a specific task in a program. In TypeScript, functions can be declared using the function
keyword.
function add(x: number, y: number): number {
return x + y;
}
In the example above, we declare a function called add
that takes two parameters of type number
and returns a value of type number
. We also use the :
operator to specify the return type of the function.
TypeScript also supports arrow functions, which are a shorthand way of declaring functions.
const add = (x: number, y: number): number => x + y;
In the example above, we declare a function called add
using an arrow function. The function takes two parameters of type number
and returns a value of type number
.
Functions can also have optional parameters and default values.
function greet(name: string, message: string = "Hello"): string {
return `${message}, ${name}!`;
}
console.log(greet("John")); // Hello, John!
console.log(greet("John", "Hi")); // Hi, John!
In the example above, we declare a function called greet
that takes two parameters: name
and message
. The message
parameter has a default value of "Hello"
. We also use string interpolation to return a greeting message.
Conclusion
Congratulations! You have learned the fundamentals of TypeScript, including variables, types, and functions. These concepts are essential for writing TypeScript code and building large-scale applications.
In the next article, we will cover more advanced topics, including classes, interfaces, and modules. Stay tuned!
Editor Recommended Sites
AI and Tech NewsBest Online AI Courses
Classic Writing Analysis
Tears of the Kingdom Roleplay
Flutter Assets:
Speech Simulator: Relieve anxiety with a speech simulation system that simulates a real zoom, google meet
Kubernetes Management: Management of kubernetes clusters on teh cloud, best practice, tutorials and guides
Declarative: Declaratively manage your infrastructure as code
Music Theory: Best resources for Music theory and ear training online