JavaScript OMG! #1 to #5

The Blurb: This is mostly just a bit of fun and in form of some rough notes as I’ve been sticking my head into a few JavaScript books and thought I’d write down what strikes me as odd/interesting along the way. Of course, I’ve written some bits of JavaScript but I’ve never really sat down and read up on it. I’m not trying to be definitive and nor am I trying to take a particular view – just poking around Smile

OMG #1 – Optional Semi-Colons

This was my first real “we’re not in Kansas any more” kind of moment with JavaScript as I hadn’t realised that it was ok to leave out semi-colons and have the interpreter try and figure out where the end of statements are based on line breaks. That is, I can write;

// either
var a = 101;
var b = 201;

// or
var c = 301
= 401
alert(a + b + c + d);

It appears that there are a bunch of rules (and, clearly, a bunch of edge cases) to remember if you want to go with the idea of leaving out semi-colons and relying on white-space so I guess what I draw from this is that I’d always put explicit semi-colons everywhere myself. The one that I particularly thought I would fall foul of would be;

function square(x)
    x * x


which is a very different function from;

function square(x)
  return x * x


purely because of the whitespace Confused smile and so I guess the only way I’m going to feel safe is;

function square(x)
  return(x * x);


Reference: Flanagan, “JavaScript The Definitive Guide”, Page 25

OMG #2 – JavaScript Type System

JavaScript types are primitive types or object types. To me, this feels a little like .NET where we have value types and reference types although I know it’s not the same thing.

Primitive types are Number, Boolean and String and Number is a 64-bit floating point value so that would make for some challenges in building scientific/financial applications and there’s nothing like .NET’s Decimal type to help out.

There’s also two other values null and undefined but they are considered to be the sole members of their own types.

That doesn’t feel too hard to get lodged into my head but it’s a clear diversion from the C# world.

Reference: Flanagan, “JavaScript The Definitive Guide”, Page 29

OMG #3 – JavaScript and Underflow, Overflow, Div Zero

This was a real surprise to me. In JavaScript;

var x = 100 / 0;


provides a value of Infinity rather than any kind of error or exception although if we divide 0 by 0;

var x = 0 / 0;


then we get NaN rather than an infinite value. It feels like the functions isNan() and isFinite() are massive helpers here in trying to determine whether you’ve got a decent value or not to work with.

Reference: Flanagan, “JavaScript The Definitive Guide”, Page 33

OMG #4 – JavaScript and RegExp

I genuinely didn’t know that JavaScript had built in support for regular expressions, making it a part of the language. That’s pretty cool and so being able to define expressions and make use of them so natively is neat.

var s = "the quick brown fox jumps over the lazy dog";

var vowels = /[aeiou]/g;

alert("Number of vowels is " + s.match(vowels).length);

You gotta like that – almost made me feel like it was back to the old days of sed and awk Smile

Reference: Flanagan, “JavaScript The Definitive Guide”, Page 39

OMG #5 – Truthy and Falsy

It sounds a little like “itchy and scratchy”. Coming straight from C# you have very fixed ideas about how you can make use of bool values in that you can assign them literal values;

bool b = true;

and you can assign them the results of expressions that resolve to booleans;

     int i = 22;

      bool b = (i == 0);

but you can’t do what you could do in C++ and use FALSE as a pseudonym for zero and any other values as a pseudonym for non-zero so in C# you can’t do something like;

      object reference = null;

      if (reference) // no-no in C#


There isn’t that conversion from non-boolean types to booleans. The JavaScript version of this story feels like “C++ and then some” in that you have the situation where everything is true unless it’s one of;

undefined, null, 0, –0, Nan, “”

you can definitely feel where this is coming from but I think it’s going to take a little getting used to for me and, especially, when you get into the various rules via which JavaScript is prepared to do conversions from one type to another Smile

Reference: Flanagan, “JavaScript The Definitive Guide”, Page 40

To be continued – you can find the rest of these “JavaScript OMG!!” entries here.