Careeroza
TypeScript vs JavaScript
Side by Side
| JavaScript | TypeScript | |
|---|---|---|
| Typing | dynamic: types are checked while running | static: types are checked before running |
| Errors found | at runtime, often by users | in the editor and at build time |
| File extension | .js, .jsx | .ts, .tsx |
| Runs directly in browser / Node | yes | no, compiled to JS first (Node 22+ can strip simple types) |
| Refactoring large code | risky | safe: the compiler shows every place to fix |
| Learning curve | smaller | a little more to learn, pays off in big projects |
The Classic JavaScript Bug
In JavaScript, "5" + 10 gives "510", not 15. Such bugs reach production because nothing checks them. TypeScript makes the intent clear.
Example
The function says it wants numbers. Calling it with a string from a form field is now a compile error, so you are forced to convert it first.
function addToCart(currentTotal: number, price: number): number {
return currentTotal + price;
}
const priceFromForm = "499"; // values from inputs are strings
console.log(addToCart(1000, Number(priceFromForm)));
console.log(addToCart(1000, parseFloat("99.5")));Output
1499 1099.5
Typos in Property Names
Example
A misspelled property silently gives undefined in JavaScript. TypeScript reports TS2551 and even suggests the right name.
const product = { name: "Wireless Mouse", price: 799 };
console.log(product.prise);Compiler output (tsc)
Line 3: error TS2551: Property 'prise' does not exist on type '{ name: string; price: number; }'. Did you mean 'price'?