Thursday, February 16, 2017

Top 5 funny things about JavaScript

The year was 1995, Google wasn't born yet. Mark Zuckerberg was 10 years old. Javascript was making its first steps. 22 years latter and JavaScript is everywhere. It's probably one of the most simple programming languages around and it's a good choice for someone that wants to start programming. However, there are some funny things about Javascript that anyone entering the language might find odd. Here's my top 5 funny things about the language. Just open a Google Chrome window and press F12. Have fun.

#5 – null is not an object?
What does a null look like in JavaScript? Well, let's check:

null is not an object

Object? Shouldn't null be the absence of meaningful value? Well, yes. Despite the above result, null is not considered an instance of an object:

null is not an object

Looking at the language specification, you can see that:
44.3.9 *undefined value*: primitive value used when a variable has not been assigned a value.
4.3.11 *null value*: primitive value that represents the intentional absence of any object value.

So, it's a case of misinterpretation.

null is not an object


#4 – NaN is a Number
What's "Not a Number"? Well... a number!

NaN is a Number

Funny as this might look, there's more. Not a Number has "identity issues" and is not equal to itself:

NaN is a Number

Well, the technical explanation for this is complex and it's related to the types of NaN (quiet NaN and signaling NaN). You can read more here, but the real way to check for a number is using the function isNaN():

NaN is a Number


#3 – Math.min() > Math.max()
Hum... So, the minimum value is higher than the max?

Math.min() Math.max()

Let's look at what they "represent":

Math.min() Math.max()

Well, seems to be wrong. These don't represent the max or min values for a number, but actually functions that given two numbers return the max or the min of the provided parameters.

Math.min() Math.max()

But why Infinity? And, apparently, in the inverse order? Well, looking at min(), "all numbers that are lower than positive infinity should be the smallest from a list, if there aren't smaller". So, the below makes sense:

Math.min() Math.max()

Number 5 is the minimum between 5 and positive Infinity.


#2 – true + true === 2
Let's do the math:

true + true === 2

So, someone entering the language might think that true === 1. Lets check:

true + true === 2

As odd as it might look, it actually makes sense. "EcmaScript standard specifies that unless either of the arguments is a string, the + operator is assumed to mean numeric addition and not string concatenation." So, it was the sum of the conversion to integer. As for the second part, true === 1 yields false because it's also comparing type and

true + true === 2



#1 – 0.1 + 0.2 !== 0.3
This is the coolest one and it's not a bug or anything. And it actually happens on several other programming languages, like C# (.NET) and I've written about it in the past

0.1 + 0.2 !== 0.3

"Computers can only natively store integers, so they need some way of representing decimal numbers. This representation comes with some degree of inaccuracy. That’s why, more often than not, 0.1 + 0.2 !== 0.3."
You can read more here (Floating Point Math) and see the behavior in other languages as well.

There is more to it, but these are definitely my favourite. If you want more, there's a cool video about JavaScript:

JavaScript Like It's 2013 (OutSystems)


Here's the full presentation at OutSystems Next Step 2013.


What's your favourite JavaScript funny fact?