Skip to main content

TypeScript vs JavaScript? Are they same?

ยท 3 min read

An interface can be thought of as a blueprint for an object or a class, specifying the properties and methods that must be implemented.

TypeScript vs JavaScript ๐Ÿค”โ€‹

TypeScript and JavaScript are related but not the same. JavaScript is a widely used programming language that allows you to create dynamic content and features for web applications. It is an interpreted language and runs on the client-side, as well as server-side using platforms like Node.js.

TypeScript is a superset of JavaScript, which means that it extends JavaScript by adding optional static types. These types help developers catch errors early in the development process and make code easier to maintain and understand. TypeScript code is transpiled (compiled) into JavaScript before it can be run in a browser or a JavaScript runtime like Node.js.

In summary, TypeScript is an superset of JavaScript that adds optional static types, which can help improve code quality and maintainability. However, TypeScript must be transpiled into JavaScript before it can be executed.

TypeScript Course Instructor Image
TypeScript Course Instructor Image

Time To Transition From JavaScript To TypeScript

Level Up Your TypeScript And Object Oriented Programming Skills. The only complete TypeScript course on the marketplace you building TypeScript apps like a PRO.

SEE COURSE DETAILS

How TypeScript is different from JavaScript ๐Ÿค”โ€‹

Here's a simple example to illustrate the difference between JavaScript and TypeScript. First, let's look at a JavaScript code snippet:

JavaScript (example.js):

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

const result = add(1, 2);
console.log(result);
// Output: 3

Now let's look at a TypeScript version of the same code snippet:

TypeScript (example.ts):

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

const result = add(1, 2);
console.log(result);
// Output: 3

The difference between the two code snippets is the addition of type annotations in the TypeScript version. In the TypeScript code, we define the types of the input parameters a and b as number, as well as the return type of the function as number. These type annotations help catch potential type-related errors during development.

For example, if you tried to call the add function with a string instead of a number in the TypeScript version, you would get a compile-time error:

const result = add(1, "2");
// Error: Argument of type 'string' is not assignable to parameter of type 'number'.

In contrast, the JavaScript version would not throw an error, and you would get unexpected output:

const result = add(1, "2");
// Output: "12"

This example demonstrates how TypeScript's type system can help catch errors early in development, improving code quality and maintainability.

What Can You Do Next ๐Ÿ™๐Ÿ˜Šโ€‹

If you liked the article, consider subscribing to Cloudaffle, my YouTube Channel, where I keep posting in-depth tutorials and all edutainment stuff for ssoftware developers.

YouTube @cloudaffle