DEV Community

Cover image for JavaScript Struggles - Part 2 | Numbers
‘Abdelraḥman Dwedar 👨🏻‍💻🇵🇸
‘Abdelraḥman Dwedar 👨🏻‍💻🇵🇸

Posted on • Updated on

JavaScript Struggles - Part 2 | Numbers

I personally didn't understand how do JS act with number, but after I learnt about it.

We have to understand what's the data types of JavaScript, and how to use them. Let me help you with it. 😄
Let's gooo

In JS we have three data types for numbers: Number, null, NaN.

I'll explain each of them and give an example for each of them.

  • Number: The main Number data type.
  • null: intentional absence of any object value.
  • NaN: Not a Number.

Number

whether it's integer like: 1
or float like: 1.3

It is number, if there was a decimal number it'll add it, if not it'll sow up as normal integer.

console.log(5/2); // Outputs: 2.5
console.log(4/2); /// Outputs: 2
Enter fullscreen mode Exit fullscreen mode

We usually use Number() to turn the variable to number.


NULL

Null basically is none. If you made undefended variable it'll equal null.

And it doesn't mean that it's an empty string or 0.

let nothing; // This variable is undefined.

console.log(null == nothing); // Outputs: true
console.log(null == 0); // Outputs: false
console.log(null == ""); // Outputs: false
Enter fullscreen mode Exit fullscreen mode
That happens because null only equals the undefined variable. null and undefined are equal but not identical.

Null usage

We can use null data type as 0, since it's an object.
Let's try it:

the_number = null; 
the_number++; // Add 1 to the null variable

console.log(the_number); // Outputs: 1

So we can use null it instead of 0.

We can add that null is an object data type


NaN

NaN stands for Not a Number, and it appears if you you have a filled string and converted it to number.

let thing = "Something";

console.log(Number(thing)); // Outputs: NaN
Enter fullscreen mode Exit fullscreen mode

But if you compared NaN with other NaN it'll return
false.

let thing = "Something";

console.log(Number(thing)); // NaN
console.log(Number(thing) == NaN); // false
Enter fullscreen mode Exit fullscreen mode

How to fix this?

Use the isNaN() method instead.

let something = "Value";

console.log(isNaN(something)); // True
console.log(isNaN(Number(something))); // True 

Discussion (4)

Collapse
lukeshiru profile image
Luke Shiru

I see a lot of misconceptions. I assume you're learning JS and have experience with another language. Here:

  • null isn't a number. You might be confused because null and 0 are both falsy values.
  • The actual numeric types are number and bigint. NaN is just a of type number.
  • To check if a value is a NaN you have the isNaN function.

Cheers!

Collapse
abdelrahman_dwedar profile image
‘Abdelraḥman Dwedar 👨🏻‍💻🇵🇸 Author

I know all of those, but I wanted to explain them in simple way and examples.
So I meant to do the wrong way so I show that it's wrong and explain the right way and the reason for it as much as I could.

null is not a number but when we set it as the value of the variable if we increased it, it'll be 1.

The last one I already said in the article right here ↴
pic

I hope you read the article carefully again. 😊

Collapse
lukeshiru profile image
Luke Shiru

I did read it carefully, it is still wrong. If you add 1 to null, null being falsy when casted to number returns 0, so adding 1 obviously returns 1.

You might want to consider actually learning about how types work in JS before "explaining it" 😑

Thread Thread
abdelrahman_dwedar profile image
‘Abdelraḥman Dwedar 👨🏻‍💻🇵🇸 Author

I didn't mean that null is number buddy. and I just added null not because it's a number, it was because people think it's equal to 0 and then get confused when it doesn't equal.

And thanks for clarifying those topics that I didn't cover.