Typecasting
What is typecasting?
for security and data integrity, is to typecast known formats. Since JavaScript is dynamically typed, a value can be any type. By typecasting the value, you can verify that the data matches are what you expect. Recall the previous MySQL example. If the ID came from a variable, it would make sense to typecast it, only if you know it should always be an integer, like this:
In Our case we know that id is integer because we assign id = 1001. but suppose it coming from user input and user can send anything because of that sometime it generate error like type error and brake application. i sure that you faced compile and run time error due to wrong type.
JavaScript Data Types
In JavaScript there are 5 different data types that can contain values:
string
number
boolean
object
function
There are 6 types of objects:
Object
Date
Array
String
Number
Boolean
Lastly, there are two data types that cannot contain values:
null
undefined
This is helpful not only when dealing with your database, but also throughout your application. Even though JavaScript is dynamically typed, you can still enforce typing in certain places.