Lesson 1 of 49 beginner

JavaScript vs TypeScript

Adding Superpowers to JS

Open interactive version (quiz + challenge)

Real-world analogy

JavaScript is like texting without autocorrect — fast but you make typos that nobody catches until it's embarrassing. TypeScript is like texting WITH autocorrect AND grammar check — it catches your mistakes BEFORE you hit send!

What is it?

TypeScript is a superset of JavaScript created by Microsoft. It adds static type checking — you declare what type of data each variable, parameter, and return value should be (string, number, boolean, etc.) and the compiler verifies you use them correctly. You also get interfaces to define object shapes, optional properties with ?, and much better editor autocomplete.

Real-world relevance

Almost every major company uses TypeScript now: Airbnb, Slack, Stripe, and more. NestJS is built entirely in TypeScript, and React has first-class TypeScript support.

Key points

Code example

// JavaScript — no types, YOLO mode 🎲
function greet(name) {
  return "Hello " + name.toUpperCase();
}
greet(42); // 💥 Runtime error! 42.toUpperCase is not a function

// TypeScript — safe mode 🛡️
function greet(name: string): string {
  return "Hello " + name.toUpperCase();
}
greet(42); // ❌ Compile error! Argument of type 'number'
           //    is not assignable to parameter of type 'string'
greet("World"); // ✅ "Hello WORLD"

// TypeScript interfaces — like a contract 📝
interface User {
  name: string;
  age: number;
  email?: string; // optional (the ? means it's OK to skip)
}

const user: User = {
  name: "Alex",
  age: 25,
  // email is optional, so this is fine!
};

Line-by-line walkthrough

  1. 1. A comment-only line introducing JavaScript without types
  2. 2. Declaring a function called 'greet' that takes a name with no type checking
  3. 3. Returns 'Hello ' followed by the name converted to ALL CAPS
  4. 4. Closing the function
  5. 5. Calling greet with the number 42 - crashes at runtime because numbers don't have toUpperCase!
  6. 6.
  7. 7. Now the same function in TypeScript with type safety
  8. 8. Declaring greet with 'name: string' type annotation and ': string' return type
  9. 9. Same logic - returns 'Hello ' plus uppercased name
  10. 10. Closing the function
  11. 11. TypeScript catches this at compile time! Can't pass a number where string is expected
  12. 12. TypeScript's error message explaining the type mismatch
  13. 13. This works because 'World' IS a string - returns 'Hello WORLD'
  14. 14.
  15. 15. Comment introducing TypeScript interfaces
  16. 16. Opening the User interface definition
  17. 17. name must be a string - required
  18. 18. age must be a number - required
  19. 19. email is optional (the ? makes it OK to skip) - if given must be string
  20. 20. Closing the interface
  21. 21.
  22. 22. Creating a 'user' variable typed as User
  23. 23. Opening the object literal
  24. 24. Setting name to 'Alex' - satisfies string requirement
  25. 25. Setting age to 25 - satisfies number requirement
  26. 26. Comment: email is optional so skipping is fine!

Spot the bug

function greet(name: string): string {
  return "Hello " + name.toUpperCase();
}
const result: number = greet("World");
Need a hint?
Look at what the function returns versus what type the variable expects...
Show answer
The function greet() returns a string, but the variable 'result' is typed as number. Fix: change 'const result: number' to 'const result: string'.

Explain like I'm 5

Imagine you're drawing a picture. JavaScript lets you draw anything but doesn't tell you if you're using the wrong color. TypeScript is like having a helpful friend who says 'Hey, that should be blue, not red!' before you finish your drawing.

Fun fact

TypeScript was created by the same person who created C# — Anders Hejlsberg. That's why TS feels familiar to C#/Java developers!

Hands-on challenge

Open your editor and rename a .js file to .ts. Add type annotations to your variables and functions. How many bugs does TypeScript find?

More resources

Open interactive version (quiz + challenge) ← Back to course: Full-Stack Playbook