Experiment 2: Typescript

 Write a program to understand simple and special types.



Simple Types

Simple types define basic kinds of data.

// Simple Types in TypeScript let isStudent: boolean = true; // boolean type let age: number = 20; // number type let studentName: string = "Ravi"; // string type console.log("Is Student:", isStudent); console.log("Age:", age); console.log("Name:", studentName);





Special Types

Special types provide flexibility or strict control over data.

any Type

Allows any type of value (no type checking).

let data: any; data = 10; console.log(data); data = "Hello"; console.log(data); data = true; console.log(data);

o/p:


---- Simple Types ---- Is Student: true Age: 20 Name: Ravi ---- Special Type : any ---- Data (number): 10 Data (string): Hello Data (boolean): true


Write a program to understand function parameters and return types.



// 1. Function with required parameters and return type function add(a: number, b: number): number { return a + b; } // 2. Function with no return value (void) function greet(name: string): void { console.log("Hello", name); } // 3. Function with optional parameter function fullName(firstName: string, lastName?: string): string { return lastName ? firstName + " " + lastName : firstName; } // 4. Function with default parameter function power(base: number, exponent: number = 2): number { return Math.pow(base, exponent); } // 5. Function returning boolean function isEven(num: number): boolean { return num % 2 === 0; } // 6. Function returning string (conditional) function eligibility(age: number): string { return age >= 18 ? "Eligible to Vote" : "Not Eligible to Vote"; } // 7. Function returning array function getNumbers(): number[] { return [1, 2, 3, 4, 5]; } // ----------- Function


o/p


Sum: 30 Hello Ravi Full Name: Anil Full Name: Anil Kumar Power (default): 25 Power: 8 Is Even: true Eligibility: Not Eligible to Vote Numbers: [ 1, 2, 3, 4, 5 ]






Write a program to show the importance of Arrow function. Use optional, default and REST
parameters






// Arrow function with default parameter const greet = (name: string = "Guest"): void => { console.log("Hello", name); }; // Arrow function with optional parameter const getFullName = (firstName: string, lastName?: string): string => { return lastName ? firstName + " " + lastName : firstName; }; // Arrow function with REST parameters const calculateSum = (...numbers: number[]): number => { let sum = 0; for (let num of numbers) { sum += num; } return sum; }; // Arrow function returning boolean const isAdult = (age?: number): boolean => { return age !== undefined ? age >= 18 : false; }; // ------------ Function Calls ------------ greet(); // default parameter greet("Ravi"); console.log("Full Name:", getFullName("Anil")); console.log("Full Name:", getFullName("Anil", "Kumar")); console.log("Sum:", calculateSum(10, 20)); console.log("Sum:", calculateSum(1, 2, 3, 4, 5)); console.log("Is Adult:", isAdult(20)); console.log("Is Adult:", isAdult());


o/p:



Hello Guest Hello Ravi Full Name: Anil Full Name: Anil Kumar Sum: 30 Sum: 15 Is Adult: true Is Adult: false

Write a program to understand the working of typescript with class, constructor, properties,
methods and access specifiers.




class Student { // Properties with access specifiers public name: string; // accessible everywhere private rollNo: number; // accessible only inside class protected course: string; // accessible in class and derived class // Constructor constructor(name: string, rollNo: number, course: string) { this.name = name; this.rollNo = rollNo; this.course = course; } // Public method public displayDetails(): void { console.log("Name:", this.name); console.log("Roll No:", this.rollNo); console.log("Course:", this.course); } // Private method private getRollNo(): number { return this.rollNo; } // Public method accessing private method public showRollNo(): void { console.log("Roll No (via private method):", this.getRollNo()); } } // Creating object of the class let student1 = new Student("Ravi", 101, "Computer Science"); // Calling public methods student1.displayDetails(); student1.showRollNo();



o/p:


Name: Ravi Roll No: 101 Course: Computer Science Roll No (via private method): 101







Comments

Popular posts from this blog

Experiment 1: Node.js

Experiment 10: ReactJS – Render HTML, JSX, Components – function & Class

Experiment 6: ExpressJS – Routing, HTTP Methods, Middleware