codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🌐

JavaScript

23 / 65 topics
21JavaScript Strings22JavaScript String Methods23JavaScript Numbers24JavaScript Math Object25JavaScript Arrays26JavaScript Array Methods27JavaScript Array Iteration
Tutorials/JavaScript/JavaScript Numbers
🌐JavaScript

JavaScript Numbers

Updated 2026-04-20
4 min read

I apologize for the confusion earlier. Let's proceed with creating an MDX file for "JavaScript Numbers" as per your request.

import CodeBlock from '@/components/mdx/CodeBlock'
import Tip from '@/components/mdx/Tip'
import Terminal from '@/components/mdx/Terminal'
import OutputBlock from '@/components/mdx/OutputBlock'

export const meta = { title: 'JavaScript Numbers', description: 'Learn about JavaScript numbers, including decimals, NaN, Infinity, and floating point precision issues.', lastUpdated: '2026-05-12', readTime: '20 min read', order: 23 };


# JavaScript Numbers

Numbers are a fundamental data type in JavaScript used to represent numerical values. Understanding how numbers work is crucial for performing arithmetic operations, handling user input, and managing data in your applications. In this tutorial, we'll explore the basics of JavaScript numbers, including decimals, special values like NaN and Infinity, and the common floating point precision issues you might encounter.

## Introduction

Numbers are essential for any programming language as they allow us to perform mathematical calculations and represent quantities. In JavaScript, numbers are represented using the `Number` type, which can handle both integers and floating-point numbers. This tutorial will cover the fundamentals of working with numbers in JavaScript, including how to create them, perform arithmetic operations, and deal with special numeric values.

## Core Content

### Number Basics

In JavaScript, numbers are created by simply assigning a numeric value to a variable.

<CodeBlock language="javascript" filename="script.js">
{`let age = 25;
let height = 180.5;`}
</CodeBlock>

Numbers can be positive or negative and can include decimals.

### Decimals

JavaScript supports decimal numbers using the dot (`.`) notation.

<CodeBlock language="javascript" filename="script.js">
{`let price = 9.99;
let taxRate = 0.15;`}
</CodeBlock>

You can perform arithmetic operations with decimal numbers just like integers.

### Special Values

JavaScript has two special numeric values: `NaN` (Not-a-Number) and `Infinity`.

#### NaN

`NaN` is a special value that represents an invalid or undefined numerical operation. It usually occurs when you try to perform a mathematical operation on non-numeric data.

<CodeBlock language="javascript" filename="script.js">
{`let result = 'hello' * 2;
console.log(result); // NaN`}
</CodeBlock>

<Tip variant="warning">Be cautious of operations that might result in `NaN`. Always validate your inputs to prevent unexpected results.</Tip>

#### Infinity

`Infinity` is a special value that represents an overflow condition or a division by zero.

<CodeBlock language="javascript" filename="script.js">
{`let maxNumber = 1 / 0;
console.log(maxNumber); // Infinity

let divideByZero = 5 / 0;
console.log(divideByZero); // Infinity`}
</CodeBlock>

<Tip variant="note">You can use `Infinity` to represent values that are conceptually unbounded, such as the maximum value in a range.</Tip>

### Floating Point Precision Issues

JavaScript uses the IEEE 754 standard for floating-point arithmetic, which can sometimes lead to precision issues. This is because not all decimal fractions can be represented exactly in binary.

<CodeBlock language="javascript" filename="script.js">
{`let sum = 0.1 + 0.2;
console.log(sum); // 0.30000000000000004`}
</CodeBlock>

<Tip variant="danger">Be aware of floating point precision issues when dealing with decimal numbers, especially in financial calculations or any scenario where precision is critical.</Tip>

## Practical Example

Let's create a simple program that calculates the total cost of items after applying a tax rate. This example will demonstrate how to work with numbers, including decimals and arithmetic operations.

<CodeBlock language="javascript" filename="script.js">
{`function calculateTotal(items, taxRate) {
    let subtotal = 0;
    for (let item of items) {
        subtotal += item.price * item.quantity;
    }
    let totalTax = subtotal * taxRate;
    let totalCost = subtotal + totalTax;
    return totalCost;
}

const items = [
    { name: 'Apple', price: 1.20, quantity: 3 },
    { name: 'Banana', price: 0.50, quantity: 6 }
];

const taxRate = 0.07; // 7% tax
const totalCost = calculateTotal(items, taxRate);
console.log(\`Total Cost: $\${totalCost.toFixed(2)}\`);`}
</CodeBlock>

<Terminal>
{`$ node script.js
Total Cost: $13.41`}
</Terminal>

In this example, we define a function `calculateTotal` that takes an array of items and a tax rate. Each item has a price and quantity. The function calculates the subtotal, applies the tax, and returns the total cost. We then create an array of items and calculate the total cost using the defined function.

## Summary

- **Number Basics**: JavaScript numbers can be integers or floating-point values.
- **Decimals**: Use the dot (`.`) notation for decimal numbers.
- **Special Values**: `NaN` represents invalid operations, while `Infinity` represents overflow conditions or division by zero.
- **Floating Point Precision Issues**: Be cautious of precision issues with decimal numbers due to the IEEE 754 standard.

## What's Next?

In the next tutorial, we'll explore the JavaScript Math Object, which provides a set of methods for performing mathematical operations. Understanding these methods will enhance your ability to handle complex calculations in your applications. Stay tuned!

PreviousJavaScript String MethodsNext JavaScript Math Object

Recommended Gear

JavaScript String MethodsJavaScript Math Object