Back
Syntax
Study
Editor
Mode:
HTML
CSS
JavaScript
PHP
Reset
Run »
HTML / CSS / JS
// TypeScript adds types on top of JavaScript // Plain JavaScript — no type safety function addJS(a, b) { return a + b; } console.log(addJS(2, "3")); // "23" — bug, no error thrown // TypeScript — types prevent the bug function addTS(a: number, b: number): number { return a + b; } // addTS(2, "3"); // Error: Argument of type 'string' is not assignable to parameter of type 'number' console.log(addTS(2, 3)); // 5 // Basic type annotations let username: string = "Alice"; let age: number = 30; let isActive: boolean = true; let scores: number[] = [95, 87, 72]; // TypeScript infers types when you assign a value let city = "London"; // inferred as string // city = 42; // Error: Type 'number' is not assignable to type 'string' // Object with inline type annotation let user: { name: string; age: number } = { name: "Bob", age: 25, }; console.log(`${user.name} is ${user.age} years old.`);
Result
Open