JavaScript is a popular programming language. TypeScript is a JavaScript-based language. The key difference between Javascript and TypeScript is that JavaScript is a client-side scripting language and TypeScript is an object-oriented, compiled language. The object-oriented programming paradigm focuses on data abstractions rather than the data processing algorithm. It is based on two main concepts: objects and classes.
What is JavaScript?
JavaScript is the programming language for making web pages interactive. JavaScript allows you to perform form validation, apply animations, and create events.
JavaScript is a client-side scripting language. When the user opens the web browser and requests a web page, that request is sent to the web server. The web server sends simple HTML / CSS code to the web browser. Browsers like Safari, Opera, Chrome, etc. contain a JavaScript engine that interprets JavaScript code.
What is TypeScript?
TypeScript is a superset of JavaScript developed by Microsoft. It has all the functionality of Javascript. It uses the TypeScript compiler to convert TypeScript (ts) file to JavaScript (js) file. TypeScript is easier to integrate into JavaScript projects. TypeScript also provides static type checking. It allows the programmer to check and assign variables and types of functions. This feature makes it easier to read the code and prevents bugs. TypeScript has data types such as String, Number, Boolean, Null, Array, Enum, Tuple, and Generics.
The main advantage of TypeScript is that it allows you to create class-based objects. C ++ and Java programmers are familiar with concepts such as classes, objects, and inheritance. When trying to program using JavaScript, it can be difficult to apply these concepts in a JavaScript scenario. To create a class in JavaScript, a programmer must create a function. For inheritance, they must use prototypes. So, since TypeScript is class based, it is able to support inheritance, encapsulation, etc. as an object oriented programming language.
JavaScript vs Typescript example:
Suppose you want to create an add () function that takes 2 numbers and returns their sum:
In javascript
function add (nbr1, nbr2) { return nbr1 + nbr2; } add(2, 2) //4 add(2, "two") //2two
No compilation errors, but this can cause unexpected results during the program’s “run cycle”. You don’t want nbr2 to accept anything other than a number.
Typescript fixes this problem by using “types” in the variable declaration
In TypeScript
function add (nbr1: number , nbr2: number ) { //nbr1 et nbr2 doit être uniquement de type "number" return nbr1 + nbr2; } add(2, 2) //4 add(2, "two") //Erreur lors de la compilation
One of the most interesting features of Typescript is compile-time type checking.
Typescript = Javascript + type checking + Class + Inheritance + Generics etc.
Comparison table
Javascript | TypeScript |
---|---|
It is a scripting language. | It is an object oriented programming language. |
there is no type checking. | There is type checking. |
JavaScript runs on the client side. | TypeScript works both on the client side and on the server side. |
Javascript does not need a compiler. It works on the web browser. | TypeScript requires a TypeScript compiler to be converted to a JavaScript file. |
Conclusion
JavaScript is a language for creating dynamic web pages. It is a lightweight interpreted language, easy to integrate with HTML and CSS. This is useful for form validation, animation, and for adding multimedia functionality to a web page. TypeScript = JavaScript + added functionality. The difference between JavaScript and TypeScript is that JavaScript is a client-side scripting language and TypeScript is a compiled object-oriented language.
We Design, Develop & Maintain Corporate Websites to generate leads and change the way their brands connect with audiences in a digital world.
Sahil is a highly skilled and dedicated Salesforce Certified Platform Developer with a passion for creating efficient and innovative solutions within the Salesforce ecosystem. With an insatiable curiosity and a relentless drive for excellence, he has become a go-to expert in developing custom applications and functionalities on the Salesforce platform.
Comments are closed.