Numbers in JavaScript

Numbers in JavaScript

01 Feb 2024
Intermediate
3.56K Views
3 min read
Learn via Video Course & by Doing Hands-on Labs

JavaScript For Beginners Free Course

Unlike object-oriented programming languages (C++, C#, Java), JavaScript doesn't have different types of numbers, like integer, floating-point, long, double, etc. JavaScript has only one type of number that is a floating-point number.

The Number is one of the primitive data types used for using the positive or negative integer, float (decimal number), binary, octal, hexadecimal, and exponential values in JavaScript, and the numbers for the JavaScript are double-precision 64-bit binary formats like a double data type in C# and Java. It follows the international IEEE 754 standard to represent the different sets of number types.

The first character of a number type should be an integer value, and it must not be enclosed in quotation marks because it can not be represented as a string of characters.

You can write JavaScript numbers with or without decimals as given below.

<script>
var x = 5; // number without decimal
var y = 5.42; // number with decimal
</script>

A JavaScript number is a 64bit (8 bytes) floating-point number which has range of 5e-324 (negative) to 1.7976931348623157e+308 (positive).

JavaScript Numbers Accuracy

JavaScript numbers without decimal are considered accurate up to 15 digits and JavaScript Numbers with decimal are considered accurate up to 16 digits.

<script>
//Non-decimal number accuracy test
var x = 999999999999999; //15 digits
var y = 9999999999999999; //16 digits 
var z = 8999999999999999; //16 digit

console.log(x); //999999999999999
console.log(y); //10000000000000000
console.log(z); //8999999999999999

//Decimal number accuracy test
var x = 1.999999999999999; //16 digits
var y = 1.9999999999999999; //17 digits
var z = 11.999999999999999; //17 digits

console.log(x); //1.999999999999999
console.log(y); //2
console.log(z); //11.999999999999998

var x = 16.99999999999999; //16 digits
var y = 161.9999999999999; //16 digits
var z = 161.99999999999999; //17 digits

console.log(x); //16.99999999999999
console.log(y); //161.9999999999999
console.log(z); //162 
</script>

Read More: Javascript Developer Salary in India

Note

If you want to get an accurate result, take the non-decimal numbers up to 15 digits and decimal numbers up to 16 digits. Since floating-point arithmetic is not always 100% accurate.

JavaScript Special Type Numbers

JavaScript has two special types of numbers: Infinity and NaN.

<script>
console.log(typeof(Infinity)) //Number
console.log(typeof(NaN)) //Number
</script>
  1. Infinity

    A JavaScript expression can return Infinity or -Infinity value as a result. For example, when you divide a number by 0 (zero), it will return Infinity

    <script>
    console.log(5/0); //Infinity
    console.log(-5/0); //-Infinity
    </script>
    
  2. NaN - Not a Number

    A JavaScript expression can return NaN value as a result. For example, when you divide a string by a number, it will return NaN

    <script>
    console.log("Hello"/4); //NaN
    console.log("Hello"-4); //NaN
    </script>
    

Read More: 50+ Javascript Interview Questions and Answers

Note

When you will use + operator within your JavaScript expression and your expression have string values, it will work as a concatenation operator and JavaScript expression will not return NaN.

<script>
console.log("Hello"+4); //Hello4
</script>

Numbers As Objects

You can also define numbers as objects with the help of a new keyword. Using numbers as an object is not recommended since they slow down execution speed.

<script>
var x = 4; //number
var y = new Number(4); //object

console.log(typeof(x)) //number
console.log(typeof(y)) //object
</script>
What do you think?

I hope, now you have a better understanding of javascript numbers. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.

Take our Javascript skill challenge to evaluate yourself!

In less than 5 minutes, with our skill challenge, you can identify your knowledge gaps and strengths in a given skill.

GET FREE CHALLENGE

Share Article

Live Classes Schedule

Our learn-by-building-project method enables you to build practical/coding experience that sticks. 95% of our learners say they have confidence and remember more when they learn by building real world projects.
Generative AI For Software Developers Jul 20 SAT, SUN
Filling Fast
08:30PM to 10:30PM (IST)
Get Details
Angular Certification Course Jul 20 SAT, SUN
Filling Fast
06:00PM to 08:00PM (IST)
Get Details
Azure Master Class Jul 20 SAT, SUN
Filling Fast
03:00PM to 05:00PM (IST)
Get Details
ASP.NET Core Certification Training Jul 28 SAT, SUN
Filling Fast
07:00AM to 09:00AM (IST)
Get Details
Software Architecture and Design Training Jul 28 SAT, SUN
Filling Fast
05:30PM to 07:30PM (IST)
Get Details
.NET Solution Architect Certification Training Jul 28 SAT, SUN
Filling Fast
05:30PM to 07:30PM (IST)
Get Details
Azure Developer Certification Training Jul 28 SAT, SUN
Filling Fast
10:00AM to 12:00PM (IST)
Get Details
Advanced Full-Stack .NET Developer Certification Training Jul 28 SAT, SUN
Filling Fast
07:00AM to 09:00AM (IST)
Get Details
Data Structures and Algorithms Training with C# Jul 28 SAT, SUN
Filling Fast
08:30PM to 10:30PM (IST)
Get Details
Angular Certification Course Aug 11 SAT, SUN
Filling Fast
09:30AM to 11:30AM (IST)
Get Details
ASP.NET Core Project Aug 24 SAT, SUN
Filling Fast
07:00AM to 09:00AM (IST)
Get Details

Can't find convenient schedule? Let us know

About Author
Amit Kumar (Software Engineer And Author)

Experienced Software Engineer with a demonstrated history of working in the professional training & coaching industry. Skilled in Asp.net MVC, Angular, Language Integrated Query (LINQ), SQL, C++, and HTML. Strong engineering professional graduated from Sathyabama University.
Accept cookies & close this