JavaScript vs TypeScript: Key Differences
| JavaScript | TypeScript |
|---|---|
| Dynamically typed: Types are determined when the code runs. | Statically typed: Types are checked before the code runs, during the compile-time. |
| No type checking, which can lead to errors only discovered when the code is running. | Type checking is done in advance, preventing many errors before running the code. |
| Limited support for modern development tools. | Provides better integration with modern tools, such as autocomplete and real-time error detection, in editors like VSCode. |
| No interfaces or type aliases. | Supports interfaces (defines the structure or shape of an object, e.g., an object must have 'name' and 'age' properties) and type aliases (gives a custom name to a type, e.g., 'UserType' can represent an object with specific properties). |
| No abstract classes. | Supports abstract classes (provides a blueprint for other classes and prevents direct instantiation; ensures child classes implement specific methods). |
| Basic object-oriented programming support. | Enhanced object-oriented programming (OOP) features, such as access modifiers (control who can use a class property or method: 'public' means accessible to all, 'private' means accessible only within the class, and 'protected' means accessible within the class and its subclasses), abstract classes, and interfaces. |
| Refactoring can be risky because there's no automatic type checking to catch errors. | Safer refactoring is possible due to static typing, which helps identify type-related issues during code changes. |
| Errors usually show up only when the code is running, which can lead to unexpected problems. | Errors are detected earlier (during compilation), preventing issues before running the code. |
| Example: let name = 'John'; name = 123; (No error until runtime) | Example: let name: string = 'John'; name = 123; (Compilation error: Type 'number' is not assignable to type 'string') |