function greet(name: string): string {
return `Hello, ${name}!`;
}
const message = greet("Alice");
console.log(message); // Output: Hello, Alice!
const add = function (a: number, b: number): number {
return a + b;
};
const result = add(3, 5);
console.log(result); // Output: 8
const multiply = (a: number, b: number): number => a * b;
const product = multiply(4, 6);
console.log(product); // Output: 24
function greetWithMessage(name: string, message: string = "Hello"): string {
return `${message}, ${name}!`;
}
const welcome = greetWithMessage("Bob");
console.log(welcome); // Output: Hello, Bob!
function getLength(input: string): number;
function getLength(input: any[]): number;
function getLength(input: any): number {
if (typeof input === "string") {
return input.length;
} else if (Array.isArray(input)) {
return input.length;
} else {
return 0;
}
}
console.log(getLength("Hello")); // Output: 5
console.log(getLength([1, 2, 3])); // Output: 3