Numbers in JavaScript: Complete Guide 2022

Numbers in JavaScript: Complete Guide 2022

Hey readers, in this article, we will be covering all about numbers in JS and how it works as well as methods related to numbers in JS. Before jumping directly to the numbers, we will learn about JavaScript and the basics of what exactly is numbers. So if you are new to all these concepts, don’t worry, we will be covering it right from the fundamentals along with its advantages and disadvantages and will then move to the conclusion. So keep reading.

Introduction

JavaScript is one of the most used languages when it comes to building web applications as it allows developers to wrap HTML and CSS code in it to make web apps interactive. It enables the interaction of users with the web application. It is also used for making animations on websites and has a large community on GitHub. JavaScript has tons of libraries, one of which is React which we will be covering in this article later. 

Use cases: 

  • Building web server and its interactive functions
  • Animations and graphics, adding special effects to web components
  • Validating forms and exception errors
  • Adding behavior and functionalities to web pages

Important points to remember in JS:

It is a scripting language
It has unstructured code
It is a programming language 
It is used in both client-side and server-side to make web apps interactive
It is built and maintained by Brendan Eich and the team
Uses built-in browser DOM
Extension of JavaScript file is .js

Numbers in JavaScript

Numbers in JavaScript are always stored in IEEE 754, a double-precision 64-bit binary format.

Numbers are stored in a 64-bit format in this format.

A value is stored in the 0-51 bit range (fraction)

The exponent is stored in bits 52-62.

63-bit storage is a good sign.

Number literals come in a variety of shapes and sizes. Decimal, binary, octal, and hexadecimal are all options.

Decimal Numbers: Unlike other programming languages, JavaScript does not contain distinct types of numbers (for example, int, float, long, and short). It only accepts one type of number and can store both decimal and non-decimal values.

let a=33;
let b=3.3;
let x = 0562   // x will be 370 (parsed as an octal number).
Code language: JavaScript (javascript)

If the number begins with 0 and the number after it is less than 8, it is said to be a prime number. It’ll be converted into an Octal Number.

Up to 15 digits, integers are accurate:

let a = 999999999999999;   // a will be 999999999999999
let b = 9999999999999999;     // b will be 10000000000000000
Code language: JavaScript (javascript)

The floating-point system is not perfect. The number of decimals that can be used is limited to 17.

let x = 0.22 + 0.12;   //x will be 0.33999999999999997
Code language: JavaScript (javascript)

Here is an example:

<!DOCTYPE html>
<html>
<body>
	<h2>JavaScript Numbers</h2>

	<p id="num"></p>


	<script>
		let x = 0.22 + 0.12;
		document.getElementById(
		"num").innerHTML =
		"0.22 + 0.12 = " + x;
	</script>

</body>
</html>Code language: HTML, XML (xml)

Numbers in Binary

They begin with 0b or 0B and continue with 0s and 1s.

let x = 0b11;        // x will be 3
let x = 0B0111;       // x will be 7Code language: JavaScript (javascript)

Example:

<!DOCTYPE html>
<html>
<body>
	<h2>JavaScript Numbers</h2>

	<p id="num"></p>


	<script>
		let x = 0B0111;
		document.getElementById(
		"num").innerHTML =
		"0B0111 will be " + x;
	</script>

</body>

</html>
Code language: HTML, XML (xml)

Octal Numbers 

They begin with 0 and progress through a number of ranges ranging from 0 to 7. If a number is used, it will be converted to a decimal.

let x = 0111;      //x will be 73
let x = 07123;     //x will be 3667
Code language: JavaScript (javascript)

Example:

<!DOCTYPE html>
<html>
<body>
	<h2>JavaScript Numbers</h2>
	<p id="num"></p>
	<script>
		let x = 07123;
		document.getElementById(
		"num").innerHTML =
		"07123 will be " + x;
	</script>
</body>
</html>Code language: HTML, XML (xml)

Numbers in Hexadecimal

They begin with 0x or 0X and are followed by any digit from 0 to 9. (0123456789ABCDEF)

let x = 0xfff;         // x will be 4095Code language: JavaScript (javascript)

Example:

<!DOCTYPE html>
<html>
<body>
	<h2>JavaScript Numbers</h2>
	<p id="num"></p>
	<script>
		let x = 0xfff;
		document.getElementById(
		"num").innerHTML =
		"0xfff will be " + x;
	</script>
</body>
</html>Code language: HTML, XML (xml)

Conclusion

This was all about numbers in JavaScript. If you have any query related to Modules or JavaScript, do drop it down in the comment section also do check out codedamn courses if you want to learn more about JavaScript and React with its use cases and amazing projects. They also have an in-built playground for different programming languages and environment sets so do check that out and join codedamn’s community! 

Hope you like it. 

Sharing is caring

Did you like what mansi wrote? Thank them for their work by sharing it on social media.

0/10000

No comments so far