Lesson 3 of 60 – TypeScript vs JavaScript
5%

TypeScript vs JavaScript

JavaScript is a popular programming language used to create interactive web applications. TypeScript is built on JavaScript and adds features such as static typing, interfaces, and better type checking.

Note: TypeScript is not a completely different language from JavaScript. It extends JavaScript with additional development features and is compiled into JavaScript before execution.

1. What is JavaScript?

JavaScript is a programming language widely used for building interactive websites and applications.

let name = "Rahul";

console.log(name);

JavaScript can run in browsers and in environments such as Node.js.

2. What is TypeScript?

TypeScript is a programming language based on JavaScript. It adds a static type system and other features that help developers write and maintain larger applications.

let name: string = "Rahul";

console.log(name);

3. Main Relationship Between TypeScript and JavaScript

The main relationship can be represented as:

JavaScript
     ↑
TypeScript adds additional features
     ↑
TypeScript code
     ↓
JavaScript

TypeScript extends JavaScript rather than replacing it.

4. Static Typing

One of the biggest differences is type checking. TypeScript allows you to explicitly specify the type of a value.

let age: number = 20;

JavaScript does not require this type annotation.

let age = 20;

5. Type Checking in TypeScript

TypeScript can detect incorrect assignments during development.

let age: number = 20;

age = "Twenty";

The second assignment causes a type error because age is declared as a number.

6. JavaScript Variables

JavaScript variables do not normally require explicit type declarations.

let value = 100;

value = "Hello";

JavaScript allows the variable to hold a value of a different type.

7. TypeScript Variables

TypeScript can specify the expected type of a variable.

let value: number = 100;

Now TypeScript expects value to contain a number.

8. File Extensions

JavaScript files normally use the .js extension.

app.js
script.js

TypeScript files normally use the .ts extension.

app.ts
student.ts

9. TSX and JSX

React applications can use JSX with JavaScript. Such files commonly use the .jsx extension.

App.jsx

When JSX is used with TypeScript, the file normally uses the .tsx extension.

App.tsx

10. Compilation

JavaScript can generally be executed directly by JavaScript environments. TypeScript needs to be transformed into JavaScript before normal execution.

TypeScript
     ↓
TypeScript Compiler
     ↓
JavaScript
     ↓
Execution

11. TypeScript Compiler

The TypeScript compiler is commonly called tsc. It checks TypeScript code and can generate JavaScript output.

tsc app.ts

This command compiles the TypeScript file.

12. Error Detection

TypeScript can detect many type-related problems while developing an application.

function add(a: number, b: number): number {
    return a + b;
}

add(10, "20");

TypeScript can identify that the second argument does not match the expected number type.

13. JavaScript Function

A JavaScript function can be written without parameter type annotations.

function add(a, b) {
    return a + b;
}

The developer does not explicitly specify the parameter types here.

14. TypeScript Function

TypeScript allows parameter and return types to be specified.

function add(
    a: number,
    b: number
): number {
    return a + b;
}

This makes the expected input and output clear.

15. JavaScript Objects

JavaScript allows objects to be created without explicitly describing their structure.

let student = {
    name: "Amit",
    age: 20
};

16. TypeScript Object Types

TypeScript can describe the expected structure of an object.

let student: {
    name: string;
    age: number;
} = {
    name: "Amit",
    age: 20
};

This helps maintain consistent object structures.

17. Interfaces in TypeScript

TypeScript provides interfaces for describing the structure of objects.

interface Student {
    name: string;
    age: number;
}

let student: Student = {
    name: "Amit",
    age: 20
};

Interfaces are not a standard JavaScript type-system feature.

18. Autocomplete and Editor Support

TypeScript provides editors with information about the types and structure of the code.

This can improve:

  • Autocomplete
  • Code suggestions
  • Error highlighting
  • Navigation
  • Refactoring

19. JavaScript Flexibility

JavaScript is dynamically typed, which gives developers flexibility when working with values.

let value = 10;

value = "Hello";

The variable can be assigned a different type of value.

20. TypeScript Type Safety

TypeScript allows developers to define expected types and detect many incorrect operations during development.

let price: number = 500;

// Type error
price = "Five Hundred";

This can help prevent certain mistakes before the application runs.

21. Learning TypeScript After JavaScript

JavaScript knowledge makes learning TypeScript easier because TypeScript is based on JavaScript.

You can gradually add TypeScript concepts such as:

  • Type annotations
  • Interfaces
  • Type aliases
  • Generics
  • Union types

22. TypeScript with React

TypeScript can be used with React to provide types for components, props, state, events, and other application data.

interface User {
    name: string;
    age: number;
}

The interface can describe the structure of data used by a component.

23. TypeScript with React Native

TypeScript is also commonly used with React Native applications. It can help describe the types of component properties, navigation parameters, API responses, and application state.

interface User {
    id: number;
    name: string;
}

24. Example: JavaScript vs TypeScript

JavaScript
function greet(name) {
    return "Hello " + name;
}
TypeScript
function greet(name: string): string {
    return "Hello " + name;
}

The TypeScript version explicitly describes the parameter and return type.

25. Example: Arrays

JavaScript
let numbers = [10, 20, 30];
TypeScript
let numbers: number[] = [10, 20, 30];

The TypeScript version explicitly states that the array contains numbers.

26. Example: Function Parameters

JavaScript
function multiply(a, b) {
    return a * b;
}
TypeScript
function multiply(
    a: number,
    b: number
): number {
    return a * b;
}

TypeScript makes the expected parameter and return types explicit.

27. JavaScript vs TypeScript Table

Feature JavaScript TypeScript
Extension .js .ts
Static Type System No Yes
Type Annotations No Yes
Interfaces No Yes
Compilation Generally not required Compiled/transformed to JavaScript
React Support Yes Yes
React Native Support Yes Yes

28. Which One Should You Learn?

JavaScript is an essential language for web development and is also important for React and React Native development.

TypeScript builds on JavaScript knowledge. Therefore, learning JavaScript first and then learning TypeScript provides a strong foundation.

For React Native: Learn JavaScript fundamentals first, then use TypeScript to build more structured and type-safe applications.

29. Simple Comparison Example

JavaScript:

let studentName = "Rahul";
let marks = 85;

console.log(studentName);
console.log(marks);

TypeScript:

let studentName: string = "Rahul";
let marks: number = 85;

console.log(studentName);
console.log(marks);

Both examples perform the same basic task, but TypeScript explicitly specifies the data types.

30. Final Difference to Remember

The easiest way to remember the difference is:

JavaScript
= Dynamic typing + JavaScript features

TypeScript
= JavaScript + Static typing + Additional type features
Remember: TypeScript does not replace the need to understand JavaScript. TypeScript builds on JavaScript and adds a type system and additional development features.

📌 Key Points

  • JavaScript is a programming language used for web and application development.
  • TypeScript is built on JavaScript.
  • TypeScript adds static typing and type checking.
  • JavaScript files normally use the .js extension.
  • TypeScript files normally use the .ts extension.
  • React TypeScript files commonly use .tsx.
  • TypeScript code is transformed into JavaScript.
  • TypeScript supports interfaces and other type-system features.
  • TypeScript can be used with React.
  • TypeScript can be used with React Native.

🧠 Quick Quiz

Question: What is the main feature TypeScript adds to JavaScript?