Skip to content

Javascript

March 1, 2007

Most people hate Javascript. I can’t really blame them but I think a lot of the bad feeling comes from:

  • Browser bugs
  • Browser inconsistency
  • Poor support for debugging/development (not as much of a problem now)
  • Lack of a structured approach when learning the language (most people learn just enough of the language to get by)

However as a language I don’t mind Javascript so much. Still Javascript does have some weird and unfortunate stuff in it. Take this expression for example:

1 == "1" => true

The equality operator automatically converts the type before the comparison. To avoid the automatic type conversion you need use the === operator.

1 === "1" => false

I’ve never minded dynamic typing much but I’m not so keen on loose typing.

Speaking of equality. Check this out:

var x = 1;
var y = 1;

x == y => true

x.a = 10;

x == y => true

A couple of interesting things here. Firstly everything has properties you can add to. Even numbers. Strange if you’re not used to it but logical and consistent when you think it through. The other interesting thing here is that adding stuff to the object doesn’t change it’s value as far as the == operator is concerned. The == operator really just looks at the value of simple types such as numbers, strings and booleans.

Objects are never equal unless they refer to the same representation.


var x = {};
var y = {};
x == y => false

but


var x = {};
var y = x;
x == y => true

Advertisements

From → Javascript

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: