Some Strange Concept of JavaScript

Jatin Sharma - Oct 25 '21 - - Dev Community

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:

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

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

foo();
Enter fullscreen mode Exit fullscreen mode

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!

function foo() {
  var undefined = 2;
  console.log(undefined); // 2
}
foo();
Enter fullscreen mode Exit fullscreen mode

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.

let a = 2 / "foo"; // NaN
typeof a === "number"; // true
Enter fullscreen mode Exit fullscreen mode

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

var a = 2 / "foo";
isNaN(a); // true
Enter fullscreen mode Exit fullscreen mode

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:

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

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:

let a = 1 / 0;
Enter fullscreen mode Exit fullscreen mode

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

var a = 1 / 0; // Infinity
var b = -1 / 0; // -Infinity
Enter fullscreen mode Exit fullscreen mode

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:

var a = 0 / -3; // -0
var b = 0 * -3; // -0
Enter fullscreen mode Exit fullscreen mode

;Let's see some more example of zeros :

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
Enter fullscreen mode Exit fullscreen mode

Null

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

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

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:

typeof null === "object"; // true
Enter fullscreen mode Exit fullscreen mode

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.

if (false)
if (null)
if (undefined)
if (0)
if (-0)
if (0n)
if (NaN)
if ("")
Enter fullscreen mode Exit fullscreen mode

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

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)
Enter fullscreen mode Exit fullscreen mode

Conclusion

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.

You can now extend your support by buying me a Coffee.😊👇

Buy Me A Coffee

Also Read

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .