First Of all, Typescript is a strongly typed "programming language" that builds on JavaScript (yep, it's a programming language 😴 ).
So what does it mean by "strongly typed"?
To explain that, we can imagine a scenario where we want to write a function in both JavaScript and TypeScript to add two numbers together
In JS the function will look like this :// function add(a, b) { return a + b; } console.log(add(5, 10)); // Output: 15 console.log(add("5", "10")); // Output: "510" (concatenation instead of addition) ///
In this , the add function doesn't specify the types of its parameters. That means we can pass a string or number to the function and if both parameters are numbers then it will add otherwise it will concatenate with each other. this is the scenario in JS.
but in Typescript, The same function will look like this :// function add(a: number, b: number): number { return a + b; } console.log(add(5, 10)); // Output: 15 console.log(add("5", "10")); // Error: Argument of type '"5"' is not assignable to parameter of type 'number'. //
[for getting the below point, just compare both the function in JS and TS ]
In this, When we created the function we already added a type annotation to the parameters (: number) and for the return value ( after the parameter, there is another ": number" that says that the function should return a number ). So In whatever scenario the function will only accept numbers. If you pass a string value to it just like you do it in JS, It won't concatenate each other instead it will give you an error.
So this is the Main difference between JS & TS. In JS , we don't explicitly set the type of parameters and what type of result the function will return and all , but in TS we explicitly set the type for each and everything just like we did in older days in JAVA and C++ ( int numb = 1 ( this only accept number)).
So you guys may ask, then why we should use Typescript??
we can discuss that in the next post 😉 .
Hope you learned something from this post! comment down if you already worked on Typescript and share the most interesting thing that you found on Typescript 😁[
#we](linkedin.com/feed/hashtag/?keywords=javascr..)bdeveloper #jsvsts #js #javascript #typescript #softwareengineer #malayalam