What is TypeScript & JavaScript?

What is TypeScript & JavaScript?

What is TypeScript?

TypeScript is a next-level version of JavaScript, often referred to as a superset of JavaScript. It is widely used in industries to make coding more structured and maintainable. While JavaScript is dynamic and flexible, TypeScript introduces additional features, primarily static typing, that make it stricter and more robust.


Key Differences Between TypeScript and JavaScript

The primary difference lies in how they handle typing:

  1. TypeScript is Statically Typed
    In TypeScript, once you define a variable with a specific type, you cannot change its type later. This is called static typing, where types are fixed and enforced.

  2. JavaScript is Dynamically Typed
    JavaScript allows you to change the type of a variable at any point. This flexibility is called dynamic typing.


What Does Typing Mean in Programming?

In simple terms, "typing" refers to the type of data a variable can hold, such as numbers, strings, or Booleans. Here's a detailed explanation:

  • Dynamic Typing (JavaScript)
    In JavaScript, you can change the type of a variable by reassigning it a new value of a different type.

    Example:

      javascriptCopy codelet a = 10;       // 'a' is initially a number
      a = "Hello";      // Now 'a' becomes a string
      console.log(a);   // Output: "Hello"
    

    This flexibility is useful in some cases but can lead to unexpected bugs in larger codebases.

  • Static Typing (TypeScript)
    In TypeScript, once you declare a variable with a specific type, you cannot change its type later.

    Example:

      typescriptCopy codelet a: number = 10; // 'a' is declared as a number
      a = "Hello";        // Error: Type 'string' is not assignable to type 'number'
    

    This rigidity ensures consistency and helps catch errors during development rather than at runtime.


TypeScript: A Strict Version of JavaScript

TypeScript introduces stricter rules and type checks compared to JavaScript. These features make it easier to manage larger projects with multiple contributors.

Key Benefits of TypeScript:

  1. Static Typing: Prevents accidental type changes and ensures consistency.

  2. Error Detection: Identifies potential issues during development, reducing runtime errors.

  3. Improved IDE Support: Offers better code completion, refactoring, and navigation features.


Quick Comparison: TypeScript vs JavaScript

FeatureTypeScriptJavaScript
TypingStatic (Fixed)Dynamic (Changeable)
Error DetectionAt compile-timeAt runtime
Use CaseSuitable for large projectsBest for smaller, flexible projects
Learning CurveSlightly SteeperEasy to get started

Example: TypeScript vs JavaScript Code

JavaScript Example:

javascriptCopy codelet age = 25;   // 'age' is a number
age = "twenty"; // No error, even though 'age' is now a string
console.log(age); // Output: "twenty"

TypeScript Example:

typescriptCopy codelet age: number = 25; // 'age' is declared as a number
age = "twenty";       // Error: Type 'string' is not assignable to type 'number'