Mastering the Magic of JavaScript Numbers: A Deep Dive into the Number Data Type

Mastering the Magic of JavaScript Numbers: A Deep Dive into the Number Data Type

Crack the Code: Elevate Your Skills and Become a JavaScript Number Ninja

ยท

4 min read

Introduction

In JavaScript, numbers are the fundamental building blocks for countless computations and operations. They enable developers to manage quantities precisely, make mathematical computations, and alter data. Understanding the JavaScript number data type is essential for releasing the full potential of this flexible programming language, from simple math to intricate algorithms.

Declaring and defining a number in javascript
const myFirstNumber = 1 or some const myFirstNumber = 1.0
The two declarations are the same since Javascript does not have a float data type.

Javascript numbers in translation to other Javascript data types

As you may know, there is more than one javascript data type. In this little section, we will be looking at how numbers translate to other types.

Boolean.
All numbers except zero 0 return true.
Say you would love to know if an array(data structure) has something in it or not.
const myFirstArray = []; if(myFirstArray.length){ console.log("It has some stuff") } else console.log("It has nothing")
The above example is proof that numbers can evaluate to booleans. So next time you have a number you can use it to make logical decisions.

Strings.
Numbers can also be expressed as Strings. And in this section, I will be looking at how you can deal with number strings as I would like to call them.
The number can be returned like this const mySecondNum = "1234"; Now let's look at how you would really tell if the returned number string is a number.

There is a method for the Number class that can help determine whether the given valuable is a number. Number.isNaN()

isNaN(NaN); // true
isNaN(undefined); // true
isNaN({}); // true

isNaN(true); // false
isNaN(null); // false
isNaN(37); // false

// Strings
isNaN("37"); // false: "37" is converted to the number 37 which is not NaN
isNaN("37.37"); // false: "37.37" is converted to the number 37.37 which is not NaN
isNaN("37,5"); // true
isNaN("123ABC"); // true: Number("123ABC") is NaN
isNaN(""); // false: the empty string is converted to 0 which is not NaN
isNaN(" "); // false: a string with spaces is converted to 0 which is not NaN

You can also return your number from a string by using a method parseInt Number.parseInt() or Number.parseFloat()

// the first argument is the number string and last one is the base
// if you do not parse in the base it returns base 10.
parseInt("0xF", 16);
parseInt("F", 16);
parseInt("17", 8);
parseInt("015", 10); // but `parseInt('015', 8)` will return 13
parseInt("15,123", 10);
parseInt("FXX123", 16);
parseInt("1111", 2);
parseInt("15 * 3", 10);
parseInt("15e2", 10);
parseInt("15px", 10);
parseInt("12", 13);

Javascript way of handling Big Numbers

The javascript number data type has a limit to it. So in this section, I will briefly look at Big Number in javascript.

Let's look at the declaration of big numbers.
Quick note: Languages like Java have a clear way of defining these numbers. So JavaScript came up with a class BigInt so here is the way to work with it.

const previouslyMaxSafeInteger = 9007199254740991n;

const alsoHuge = BigInt(9007199254740991);
// 9007199254740991n

const hugeString = BigInt("9007199254740991");
// 9007199254740991n

const hugeHex = BigInt("0x1fffffffffffff");
// 9007199254740991n

const hugeOctal = BigInt("0o377777777777777777");
// 9007199254740991n

const hugeBin = BigInt(
  "0b11111111111111111111111111111111111111111111111111111",
);
// 9007199254740991n

The good news is that these big numbers can perfectly work well the normal numbers in JavaScript so let's see that sample of that.

1n < 2; // true
2n > 1; // true
2 > 2; // false
2n > 2; // false
2n >= 2; // true

However, there is a downside to these numbers. It's a little tricky to work with operations between big numbers and normal numbers.

const result = 1 + 2n
console.log(result)

TypeError: Cannot mix BigInt and other types, use explicit conversions

So to sum this up Javascript does not allow mixing the two types. so please be conscious when working with these types.
Here is a leet code challenge about numbers I would love to share.
https://leetcode.com/problems/valid-number/ . I will share the solution in my next post please watch out.

Special links Looking to learn more, about what you need to know before you start coding

Conclusion

As we draw the curtains on our exploration of the JavaScript number data type, we leave behind a realm of numerical wonders and mathematical precision. Throughout this blog, we've delved into the versatile world of JavaScript numbers, unraveling their mysteries and discovering the hidden potential they hold within our code.

Remember, just as numbers can unlock the mysteries of the universe, they can also open the potential of your applications. Embrace the magic of JavaScript numbers, and let them be your guide to crafting remarkable software that captivates and inspires.

Thank you for joining us on this captivating journey through the enchanting world of JavaScript numbers. Happy coding, and may your future adventures in programming be filled with numerical brilliance!

Did you find this article valuable?

Support Let's learn by becoming a sponsor. Any amount is appreciated!

ย