DEV Community

Cover image for JS being odd. Some weird things about javascript.
Martín Mato
Martín Mato

Posted on

JS being odd. Some weird things about javascript.

Many of us will agree that Javascript is one of the best languages around, but sometimes some things can make us some headaches.

Here is a list of five weird things Javascript has. Please feel free to add your favorites in the comments.

null acting weird.

As the documentation said, The value null represents the intentional absence of any object value. But if I tell you null is an object? Just check by yourself.

console.log(typeof null) // log: object
Enter fullscreen mode Exit fullscreen mode

Weird, right?. But don't worry, it doesn't look so weird when we check that null is not an instance of an object, making its definition valid.

console.log(null instanceof Object) // log: false
Enter fullscreen mode Exit fullscreen mode

Side note: [] also is an object but, in that case, is an instance of an object too.

Number literals as objects

Except for null and undefined, everything in Javascript acts like an object, and number literals are not the exception.

So, try this:

console.log(10.toString())
Enter fullscreen mode Exit fullscreen mode

Is failing, right? You probably will see an error similar to this: error: unknown: Identifier directly after number (1:15).
This is happening because javascript's parser will read the . notation as a floating-point and fail. To avoid this, here are some workarounds.

console.log(10..toString()) 
console.log((10).toString())
Enter fullscreen mode Exit fullscreen mode

NaN is a number.

Yes, I know, the literal definition of NaN is "Not a Number," but:

console.log(typeof NaN); // Will return number
Enter fullscreen mode Exit fullscreen mode

and not only that

console.log(NaN == NaN) // Will return false.
Enter fullscreen mode Exit fullscreen mode

This could be because NaN is not equal to anything, being isNaN() the only way to check for it.

Be careful with parseInt()

parseInt will take any string and parse it to an integer of the specified base we pass as the second parameter.

console.log(parseInt("10", 10)); // will return 10  
console.log(parseInt("flowers")); // will return NaN
Enter fullscreen mode Exit fullscreen mode

But look if we change the base in the last statement:

console.log(parseInt("flowers", 16)); // will return 15
Enter fullscreen mode Exit fullscreen mode

Even the word "flowers" is not a valid hex parseInt() will evaluate character by character instead of the entire word, so in this case, is returning the value for the f.

Check your return

Consider the following code:

function example() {
  return
  {
    value: "string"
  }
}

console.log(example()); // Logs: undefined
Enter fullscreen mode Exit fullscreen mode

Just by being minimally familiar with javascript, you will realize something strange in that new line after the return statement.
What in many languages can be a style issue in JS has another effect. JS, behind curtains, inserts a semicolon after most newlines causing in the example that we never reach to return the object. So if we want to make it right:

function example() {
  return {
    value: "string"
  }
}

console.log(example()); // Logs: {value:"string"}
Enter fullscreen mode Exit fullscreen mode

Finishing up

These were only a few of tens of weird things Javascript has to offer. I promise I will add some other later.
Please comment on which other do you know and if they gave you a hard time.

Thanks for reading!

Top comments (2)

Collapse
 
jenbutondevto profile image
Jen

NaN has always been my favourite JS quirk. Cue some bored engineers having a conversation that goes a little like this:
dev 1: NaN is not a number
dev 2: NaN IS a number
dev 1: NaN is no-

Collapse
 
martinemanuelmaldonado93 profile image
MartinEmanuelMaldonado93 • Edited

Nan its a weird number, because IIFE system uses to express a something that it's not a number.... ahhh js I love that guy xD