wasAsked.

What is the difference between JavaScript and TypeScript?

Frequently Asked (All Companies) · Role not specified · technical

Published

Year not specified · JavaScript, TypeScript

Community answers

Posted by wasAsked editorial collection

TypeScript is JavaScript with a static type system added on top. Every valid JavaScript program is also valid TypeScript, but TypeScript lets you describe the shape of your data so mistakes are caught while you write code instead of when users run it. How they relate - JavaScript is the language browsers and Node.js actually execute. It is dynamically typed: a variable can hold a number now and a string later, and type errors appear only at runtime. - TypeScript is a superset created by Microsoft. You write .ts or .tsx files, and the TypeScript compiler (tsc) or a build tool (Vite, esbuild, SWC) removes the types and outputs plain JavaScript. Types never exist at runtime. Key differences 1. Typing JavaScript: function total(price, qty) { return price * qty; } total("100", 2); // "100" * 2 = 200 by coercion — or wrong results in other operations TypeScript: function total(price: number, qty: number): number { return price * qty; } total("100", 2); // compile error: Argument of type 'string' is not assignable to parameter of type 'number' 2. Interfaces, types and generics TypeScript adds interfaces, type aliases, union and intersection types, enums, generics, readonly modifiers and access modifiers (private, protected): interface Order { id: number; status: 'pending' | 'paid' | 'shipped'; items: OrderItem[] } 3. Tooling Because the editor knows the types, you get accurate autocomplete, safe refactoring (rename a property everywhere), inline documentation and early error detection. This is the biggest day-to-day productivity gain. 4. Compilation step JavaScript runs directly. TypeScript needs compilation or type stripping (Node.js can now run many .ts files directly by stripping types, but type checking still requires tsc). 5. Strictness is configurable tsconfig.json options such as "strict": true enable null checks (strictNullChecks), no implicit any, and stricter function checks. You can adopt TypeScript gradually in an existing JavaScript project with allowJs and JSDoc types. Advantages of TypeScript - Fewer runtime bugs, especially "undefined is not a function" and wrong property names. - Self-documenting code and safer refactoring in large codebases and teams. - Excellent support in frameworks: Angular is written in TypeScript, and React, Vue, Node.js (NestJS, Express) projects commonly use it. Advantages of plain JavaScript - No build or type-checking step, faster for small scripts and prototypes. - No time spent on type definitions for simple code. Common interview follow-ups - Types are erased, so TypeScript cannot validate data coming from APIs at runtime. Use runtime validation (zod, valibot) for untrusted input. - "any" disables type checking — prefer "unknown" and narrow it. - TypeScript does not make code faster; performance is the same because the output is JavaScript. Summary: JavaScript is the runtime language; TypeScript is JavaScript plus a compile-time type system that improves reliability and tooling, which is why most medium and large frontend and Node.js projects use it today.

Ravindra Wadile · · 0 likes

Sign in to add your answer

Related interview topics