Will JavaScript detect the keying error in the following example?
publish date: 2022/09/06 01:54:00 GMT+10
volume_mutesuppose that in a particular JavaScript program, i
and x
are currently the names of scalar numeric variables and y
is currently the name of an array. Furthermore, suppose that the program needs the assignment statement
i = x;
but because of a keying error, it has the assignment statement
i = y;
Correct Answer
Explanation
In JavaScript (or any other language that uses dynamic type binding), no error is detected in this statement by the interpreter—the type of the variable named i
is simply changed to an array. But later uses of i
will expect it to be a scalar, and correct results will be impossible. In a language with static type binding, such as Java, the compiler would detect the error in the assignment i = y
, and the program would not get to execution.
Reference
Concepts of Programming languages, 10th ed