Bad JavaScript Bug and typing

Johan thinks I was unfairly bashing untyped languages in general in this post.

Here's how I think JavaScript (or an alternative) language should best handle the following code:

var s = '7';
var num = s * 5;

The virtual machine should now know that s is of type String. The virtual machine should now halt and report an error. You shouldn't be able to multiple a String by 5. Unfortunately JavaScript automatically tries to turn the string into a number, thereby giving us 35.

The following code should be necessary:

var s = '7';
var num = parseInt(s)*5;

I suggest this not because of one JavaScript problem. I suggest it because I've seen JavaScript continue to negatively surprise programmers for 12 years.

Here's my favourite example of the mysterious workings of JavaScript:

var s = '7' + 0; // s is a string containing '70'
var t = '7' - 0; // t is a  number containing 7

The rumour is that Google will shortly be announcing a new language that runs in the browser as a replacement for JavaScript. AND runs on the server as a replacement for Java, C#, PHP, etc. In light of typing, it promises optional typing. I look forward to it.




Popular posts from this blog

WordPress comments can never be really disabled

Ask Customers for Money Already...

T-10: But Retina is Different...