JavaScript Type Systems and Type Checking
JavaScript: Type System
•
There are types and each value is of a particular type (or none)
•
But the type of a variable does not need to be declared
•
The type of a variable depends on the value it currently stores and
the type can change if it is assigned a value of a different type
•
Function declarations do not specify the type of their parameters
fun ctio n add (x ,y) { r eturn x + y; }
•
In function applications the types of arguments will be adjusted
automatically (if possible)
add (519 , 1.9 e3 ) // re turns the number 2419 = 519 +190 0
add ( ’519 ’ ,"1.9 e3 ") // returns the string ’5191.9 e3 ’
add (519 , ’1.9 e3 ’) // re turns the string ’5191.9 e3 ’
add ( true , 1.9 e3 ) // ret urns the nu mber 1901
; makes programming easier
; potentially leads to more bugs
COMP519 Web Programming Lecture 10 Slide L10 – 22