Type inference
One of the main features of JavaScript is that it's a dynamically typed language. This means a type of a variable is determined at runtime and the compiler does not generate any errors if there is a type mismatch until it fails. We can have the same variable hold a string, a number, an array, or an object and JavaScript will not complain, but it can cause errors at runtime if not handled. JavaScript uses type inference to identify the type of data stored in a variable, based on which it operates on the variable. The following is an example where type inference fails between a number and a string.
In the following code, when num is assigned a value of 1, JavaScript infers it as a number and performs addition when we try to add ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access