Hi guys, it's been a while here π. I am coming back with a new topic - Typescript vs JavaScript. I've been learning both of them for some time now, but I've decided to share my experience. Let's dive right in ππππ€.
What is JavaScript?
Javascript is a programming language created to interact with elements of a web page programmatically. Javascript has gone beyond interacting with a web page. You can also use it to build desktop and mobile apps. JavaScript is dynamically typed and compiled just in time (JIT).
NB: JavaScript is not Java
Why JavaScript?
Javascript is everywhere:
- Client (browsers) - when writing javascript for the web browser, you need a script tag that embeds your javascript code. JavaScript for web browsers is used to build a user interface.
- Server - when building backend applications, you can use JavaScript, in other words, Node.js.
- Native - Building native desktop and mobile applications with Electron, React-native, and NativeScript.
What is Typescript?
It is a superset of Javascript, meaning it's a layer around JS with more methods. That makes you follow a particular way of development that you don't have to otherwise in vanilla (like setting the types of your variables). It is a language that is built upon JS. TypeScript is statically typed, meaning you can define your variables and parameter type.
Why TypeScript?
- TypeScript adds types to your javascript code.
- It helps you identify errors early before running your code.
- It makes writing JavaScript easy and fun.
- With TypeScript, you write a clean and straightforward JS code.
- TypeScript supports JS libraries and API documentation.
- You can convert TypeScript into plain JavaScript code.
Key Differences
- JavaScript is a scripting language that helps you create interactive web pages, whereas TypeScript is a superset of JavaScript.
- TypeScript uses concepts like types and interfaces to describe data being used, whereas JavaScript has no such concept.
- JavaScript is dynamically typed while TypeScript is statically typed. TypeScript is a powerful type system, including generics and JS features for large projects, whereas JavaScript is an ideal option for small projects.
- TypeScript code needs to be compiled, while JavaScript does not need to be compiled.
TYPE
I'm sure you have been wondering what type it is, as I have made many mentions about it. Well, I will briefly introduce it and add a link where you can read more.
Type is like referring to data types in other programming languages, but it's called type in TypeScript.
In TypeScript, you can specify the type of your variables, parameters, functions, and object properties.
We can specify the type using: Type after the variable's name, parameter, or property.
TypeScript includes all the primitive types of JavaScript- number, string, and boolean.
An example of how to specify a type in typescript:
var age: number = 32; // number variable
var name: string = "John";// string variable
var isupdated: boolean = true;// Boolean variable
From the example above, we can see how the type of age was specified to be a number and that of name to be a string. That is how you specify the type of your variables, parameters, objects in Typescript.
Conclusion
JavaScript and TypeScript fit in the modern web environment perfectly. JavaScript is preferred in small applications while TypeScript is mainly used in large enterprise applications. It depends on the type of project you are working on.
I hope this guide helps you figure out the main differences between JavaScript and TypeScript.
Don't forget to likeπ, comment, and also share.
Thank you!π