JS being odd. Some weird things about javascript. 
Tags: javascript, webdev, beginners, codenewbie
Published: 2021-03-30 15:44:58

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.

``` javascript
console.log(typeof null) // log: object
```

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. 

``` javascript
console.log(null instanceof Object) // log: false
```
> 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:
```javascript
console.log(10.toString())
```
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. 

```javascript
console.log(10..toString()) 
console.log((10).toString())
```

## `NaN` is a number.

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

``` javascript
console.log(typeof NaN); // Will return number
```

and not only that

```javascript
console.log(NaN == NaN) // Will return false.
```

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.

```javascript
console.log(parseInt("10", 10)); // will return 10  
console.log(parseInt("flowers")); // will return NaN
```

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

``` javascript
console.log(parseInt("flowers", 16)); // will return 15
```

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:

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

console.log(example()); // Logs: undefined
```

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:

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

console.log(example()); // Logs: {value:"string"}
``` 

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