Some Strange Concept of JavaScript

Oct 24, 2021
5 min read
904 words

In this article, we are going to look at some weird and strange concepts of JavaScript, Some of which you may know and some may not. So stay tuned till the end.

Undefined

In non-strict mode, it's actually possible (though incredibly ill-advised!) to assign a value to the globally provided undefined identifier:

script.js

// non-strict
function foo() {
  undefined = 2; // really bad idea!
}
 
foo();

script.js

// Strict Mode
function foo() {
  "use strict";
  undefined = 2; // TypeError!
}
 
foo();

In both non-strict mode and strict mode, however, you can create a local variable of the name undefined. But again, this is a terrible idea!

script.js

function foo() {
  var undefined = 2;
  console.log(undefined); // 2
}
foo();

The not number (NaN)

Any mathematic operation you perform without both operands being numbers (or values that can be interpreted as regular numbers in base 10 or base 16) will result in the operation failing to produce a valid number, in which case you will get the NaN value.

script.js

let a = 2 / "foo"; // NaN
typeof a === "number"; // true

In other words, “the type of not-a-number is number!” Hooray for confusing names and semantics.

script.js

var a = 2 / "foo";
isNaN(a); // true

Easy enough, right? I've use the built-in global utility called isNaN(..) and it tells us if the value is NaN or not. Problem solved! Not so fast.

The isNaN(..) utility has a fatal flaw. It appears it tried to take the meaning of NaN (“Not a Number”) too literally—that its job is basically, “test if the thing passed in is either not a number or is a number.” But that's not quite accurate:

script.js

var a = 2 / "foo";
var b = "foo";
a; // NaN
b; //"foo"
window.isNaN(a); // true
window.isNaN(b); // true--ouch!

Clearly, "foo" is literally not a number. it's a string but when you do isNaN("16") then it will return false because when javascript does implicit conversion it perfectly converts to number that's why it returns false

Infinities

Developers from traditional compiled languages like C are probably used to seeing either a compiler error or runtime exception, like divide by zero for an operation like:

script.js

let a = 1 / 0;

However, in JS, this operation is well-defined and results in the value Infinity (aka Number.POSITIVE_INFINITY). Unsurprisingly:

script.js

var a = 1 / 0; // Infinity
var b = -1 / 0; // -Infinity

Zeros

While it may confuse the mathematics-minded reader, JavaScript has both a normal zero 0 (otherwise known as a positive zero +0) and a negative zero -0. Before I explain why the -0 exists, we should examine how JS handles it, because it can be quite confusing.

Besides being specified literally as -0, negative zero also results from certain mathematic operations. For example:

script.js

var a = 0 / -3; // -0
var b = 0 * -3; // -0

Let's see some more example of zeros :

script.js

var a = 0;
var b = 0 / -3;
a == b; // true
-0 == 0; // true
a === b; // true
-0 === 0; // true
0 > -0; // false
a > b; // false

Null

So as we know typeof is an operator, by which we check the type of a variable like this :

script.js

typeof undefined === "undefined"; // true
typeof true === "boolean"; // true
typeof 42 === "number"; // true
typeof "42" === "string"; // true
typeof { life: 42 } === "object"; // true

As you may have noticed, I excluded null from the above listing. It's special—special in the sense that it's buggy when combined with the typeof operator:

script.js

typeof null === "object"; // true

It would have been nice (and correct!) if it returned null, but this original bug in JS has persisted for nearly two decades, and will likely never be fixed because there's so much existing web content that relies on its buggy behavior that “fixing” the bug would create more “bugs” and break a lot of web software.

null is the only primitive value that is falsy but which also returns object from the typeof check.

BONUS

What are falsy value? A falsy (sometimes written falsey) value is a value that is considered false when encountered in a Boolean context.

script.js

if (false)
if (null)
if (undefined)
if (0)
if (-0)
if (0n)
if (NaN)
if ("")

What are Truthy value? In JavaScript, a truthy value is a value that is considered true when encountered in a Boolean context.

script.js

if (true)
if ({})
if ([])
if (42)
if ("0")
if ("false")
if (new Date())
if (-42)
if (12n)
if (3.14)
if (-3.14)
if (Infinity)
if (-Infinity)

Wrapping up

It's not the end, there are bunch of things likes this which I will cover it the future, So consider to follow. And If you learned something new then give a thumbs up.

Jatin's Newsletter

I write monthly Tech, Web Development and chrome extension that will improve your productivity. Trust me, I won't spam you.

Share on Social Media: