Javascript != C#
A while ago I blogged about the pitfalls of working in multiple languages, and lo and behold I instantly went and fell down another of them.
I had some code that I had written in C# and I needed to port it to JavaScript, I did briefly think to use Script# but it seemed like complete overkill for a class with a few calculations in. Anyway the C# code looked like this
double value1 = 10.50; double value2 = 00.50; double result = value1 + value2;
I know that the leading zero in the number assigned to value2 is not needed but it made the decimal points line up, which made the code a but easier to read (this is an excerpt, there was a large column of numbers in the real code). Well all I can say is that I should have used a space or tab but I didn’t. All was well the code compiled and passed its tests.
When I ported it to JavaScript it came out a bit like this.
var value1 = 10.50; var value2 = 00.50; var result = value1 + value2;
But when I tried to get it to pass its tests using JsUnit it didn’t pass, in fact JsUnit couldn’t load the file. Eventually I tried loading the javascript in Firefox and looked in the error console and saw
At first this confused me even more until I remembered that the Javascript octal number format was causing me problems, the feature is deprecated but still present in most browsers, and the leading zero causes the browser to assume the number is in octal. The decimal place was causing the problem in that it made the number invalid.
So once again, note to self, even though C# and JavaScript have a similar syntax, they are not the same language.