Harry Yates
I'm a web developer focused on TypeScript, React, and Three.js.
JavaScript is a dynamically typed language. This means that variable types are determined at runtime. The environment running your JavaScript code (for example, the browser or Node) does not know the data types of variables until they are being run.
JavaScript does not know the data type of the a
and b
parameters. You may assume that this is a function that receives numbers and returns the sum of both. However, that's an incorrect assumption.
The +
operator works with strings too, meaning you can call add("abc", "def")
and you will get the "abcdef"
string as a result.
This is where TypeScript comes in handy. TypeScript adds types to JavaScript.
This sort of error thrown by the TypeScript compiler is extremely helpful as it helps you reduce the amount of bugs in your code.
When a parameter is typed (for example, : number
), TypeScript will throw an error if you call the function with another incompatible type as an argument. This helps prevent bugs.