DEV Community

Cover image for JavaScript vs TypeScript: Understand the Difference With Simple Examples
Kartik Kumar
Kartik Kumar

Posted on

JavaScript vs TypeScript: Understand the Difference With Simple Examples

Introduction

If you're a JavaScript developer or just getting started in web development, you’ve probably heard of TypeScript.

Some developers love it. Some avoid it.

But what exactly is TypeScript, and how is it different from JavaScript?

Let’s break it down with simple real-world examples, and you'll get a clear picture by the end of this article.


πŸ” What is TypeScript?

TypeScript is a superset of JavaScript.

That means all valid JavaScript code is also valid in TypeScript.

But TypeScript adds features on top of JavaScript β€” most importantly: static typing.

Instead of waiting for your code to break at runtime, TypeScript can catch many common errors during development, saving time and making your codebase more stable.

TypeScript was developed by Microsoft and is now one of the most loved languages among developers.


🧠 Why Was TypeScript Created?

JavaScript is a flexible and dynamic language β€” but it doesn’t always protect you from mistakes.

For example:

let user = "John";
user = 123; // JavaScript won’t complain

## βœ… Type Checking in TypeScript
Enter fullscreen mode Exit fullscreen mode

TypeScript:

let score: number = 100;
score = "one hundred"; // ❌ Error: Type 'string' is not assignable to type 'number' ##`
Enter fullscreen mode Exit fullscreen mode

Function Parameters and Return Types

JavaScript:

function greet(name) {
  return "Hello " + name;
}

greet(123); // Still works, but it's not meaningful
Enter fullscreen mode Exit fullscreen mode

TypeScript:

function sayHello(name: string, message: string = "Welcome"): string {
  return `${message}, ${name}!`;
}
Enter fullscreen mode Exit fullscreen mode

Generics β€” Writing Reusable Code

JavaScript:

function identity(value) {
  return value;
}
Enter fullscreen mode Exit fullscreen mode

TypeScript:

function identity<T>(value: T): T {
  return value;
}

let result1 = identity<string>("Hello");
let result2 = identity<number>(42);
Enter fullscreen mode Exit fullscreen mode

πŸ’₯ Benefits of TypeScript

  • Catch bugs at compile time, not in production
  • Improve code readability and predictability
  • Enhance code editor support (autocompletion, suggestions, etc.)
  • Work better with large teams and complex applications
  • Type safety = confidence in code refactoring

πŸš€ Getting Started With TypeScript

Install it globally:

npm install -g typescript
Enter fullscreen mode Exit fullscreen mode

Create a .ts file and compile it:

tsc index.ts
Enter fullscreen mode Exit fullscreen mode

Try online:
πŸ‘‰ https://www.typescriptlang.org/play

πŸ§ͺ Practice: Convert This JavaScript Function to TypeScript

function add(a, b) {
  return a + b;
}
Enter fullscreen mode Exit fullscreen mode

➑️ Your task:

  • Add type annotations
  • Define return type
  • Try it in the playground

✨ Final Thoughts

TypeScript is like a safety net for JavaScript.
It won't stop you from writing JavaScript β€” it just makes you write better JavaScript.

If you're serious about building solid, scalable applications β€” especially with React, Node.js, or Angular β€” learning TypeScript is a great investment in your career.

πŸ™Œ Liked This Post?

Drop a ❀️ or bookmark it for later.
Follow me for more dev-friendly content around JavaScript, TypeScript, MERN stack, and real-world projects.

Let’s grow together πŸš€

---

Agar chaahe to next blog ka idea bhi de sakta hoon, jaise:

- **"TypeScript in a React Project (Beginner Setup)"**
- **"MERN Stack with TypeScript β€” Real World Setup"**

Bhai, tu isse **professional level** pe le jaa sakta hai. Agar blog ko aur polish karna hai β€” intro me emoji, heading me CTA, ya GitHub example add karwana hai to bol dena πŸ’ͺ
Enter fullscreen mode Exit fullscreen mode

Need help building your TypeScript or MERN project?

πŸ“© Email me at kumarkartik0056@gmail.com or DM on LinkedIn

Top comments (0)