🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Introduction
In this chapter, we will explore the basic syntax of TypeScript by examining a simple TypeScript program. Understanding the basic syntax is crucial for writing TypeScript code effectively.
Table of Contents
- Basic TypeScript Syntax
- Declaring Variables
- Functions
- Type Annotations
- Conclusion
Basic TypeScript Syntax
Let's revisit the simple TypeScript program we wrote in the previous chapter and break down its syntax:
// src/index.ts
function greet(name: string): string {
return `Hello, ${name}! Welcome to TypeScript.`;
}
const userName = "Ravi";
console.log(greet(userName));
Declaring Variables
In TypeScript, you can declare variables using let, const, or var. However, let and const are preferred due to their block-scoping behavior.
let: Used to declare a variable whose value can be changed.const: Used to declare a constant whose value cannot be changed.
Example
let age: number = 30; // The value of 'age' can be changed
const name: string = "Ravi"; // The value of 'name' cannot be changed
Functions
Functions in TypeScript are similar to those in JavaScript, but with added type annotations for parameters and return types.
Example
function greet(name: string): string {
return `Hello, ${name}! Welcome to TypeScript.`;
}
Explanation:
function: Keyword to declare a function.greet: Name of the function.name: string: Parameternamewith type annotationstring.: string: Return type annotation, indicating that the function returns a string.`Hello, ${name}! Welcome to TypeScript.`: Template string used to return a greeting message.
Type Annotations
Type annotations are used to specify the types of variables, parameters, and return values. They help in catching type-related errors during development.
Example
const userName: string = "Ravi";
let age: number = 30;
Explanation:
userName: string: VariableuserNameis annotated with the typestring.age: number: Variableageis annotated with the typenumber.
TypeScript uses type annotations to explicitly specify types for identifiers such as variables, functions, objects, etc.
TypeScript uses the syntax : type after an identifier as the type annotation, which type can be any valid type.
Console Output
The console.log method is used to print messages to the console. It is useful for debugging and displaying output.
Example
console.log(greet(userName));
Explanation:
console.log: Method to print messages to the console.greet(userName): Calls thegreetfunction withuserNameas the argument and prints the returned message.
Conclusion
In this chapter, we explored the basic syntax of TypeScript by breaking down a simple program. We learned how to declare variables, define functions, use type annotations, and print output to the console. Understanding these basics is essential for writing effective TypeScript code.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment