Some Strange Concept of JavaScript
Tags: javascript, webdev, beginners, tutorial
Published: 2021-10-25 07:14:13

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:

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

foo();
```

```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!

```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.

```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.

```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:

```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`

<!-- console.log(Math.pow(2, 1024)); -->

### 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:

```js
let a = 1 / 0;
```

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

```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:

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

;Let's see some more example of `zeros` :

```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 : 

```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:

```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.

```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.

```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)
```


### 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.

<blockquote>
<p>You can now extend your support by buying me a Coffee.😊👇</p>
</blockquote>
<a href="https://www.buymeacoffee.com/j471n"><img src="https://cdn.buymeacoffee.com/buttons/v2/default-yellow.png" alt="Buy Me A Coffee" loading="lazy" width="545" height="153"></a>


#### Also Read

- [Some Javascript Methods you should know](https://dev.to/j471n/some-javascript-methods-you-should-know-i15)
- [10 Git Commands everybody should know](https://dev.to/j471n/10-git-commands-everybody-should-know-3322)
- [Convert Next.js app to PWA](https://dev.to/j471n/convert-nextjs-app-to-pwa-3fd)