JavaScript Numbers example

JavaScript has a single number type that represents both integers and floating-point numbers. Unlike other languages, JavaScript doesn't distinguish between different numeric types.

Number Types in JavaScript

All numbers in JavaScript are stored as 64-bit floating-point values, following the IEEE 754 standard. This means you can work with both whole numbers and decimals seamlessly.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JavaScript Numbers</title>
    <style>
        body {
            font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
            margin: 20px;
        }
        .result {
            font-size: 18px;
            font-weight: 500;
            color: #333;
            background-color: #f5f5f5;
            padding: 15px;
            border-left: 4px solid #007acc;
            margin: 10px 0;
        }
        button {
            background-color: #007acc;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
        }
        button:hover {
            background-color: #005999;
        }
    </style>
</head>
<body>
    <h1>JavaScript Numbers Example</h1>
    <div class="result" id="output"></div>
    <button onclick="showNumbers()">Show Number Examples</button>
    <h3>Click the button to see different number types and operations</h3>
    
    <script>
        function showNumbers() {
            let outputDiv = document.getElementById("output");
            
            // Different number types
            let integer = 42;
            let decimal = 3.14159;
            let negative = -25;
            let scientific = 2.5e6;  // 2,500,000
            
            let result = `
                <strong>Number Types:</strong><br>
                Integer: ${integer}<br>
                Decimal: ${decimal}<br>
                Negative: ${negative}<br>
                Scientific: ${scientific}<br><br>
                
                <strong>Basic Operations:</strong><br>
                ${integer} + ${decimal} = ${integer + decimal}<br>
                ${integer} - ${negative} = ${integer - negative}<br>
                ${integer} * ${decimal} = ${(integer * decimal).toFixed(2)}<br>
                ${integer} / 2 = ${integer / 2}<br><br>
                
                <strong>Type Check:</strong><br>
                typeof ${integer} = ${typeof integer}<br>
                typeof ${decimal} = ${typeof decimal}
            `;
            
            outputDiv.innerHTML = result;
        }
    </script>
</body>
</html>

Output

When you click the button, you'll see:

Number Types:
Integer: 42
Decimal: 3.14159
Negative: -25
Scientific: 2500000

Basic Operations:
42 + 3.14159 = 45.14159
42 - (-25) = 67
42 * 3.14159 = 131.95
42 / 2 = 21

Type Check:
typeof 42 = number
typeof 3.14159 = number

Key Features

JavaScript numbers have several important characteristics:

  • Single Type: All numbers use the same number type
  • Precision: Can represent integers up to 2^53 - 1 safely
  • Special Values: Includes Infinity, -Infinity, and NaN
  • Automatic Conversion: JavaScript handles integer/decimal operations seamlessly

Number Literals

// Different ways to write numbers
let decimal = 123.45;
let scientific = 1.23e2;  // 123
let binary = 0b1010;      // 10 in decimal
let octal = 0o17;         // 15 in decimal  
let hex = 0xFF;           // 255 in decimal

console.log("Decimal:", decimal);
console.log("Scientific:", scientific);
console.log("Binary:", binary);
console.log("Octal:", octal);
console.log("Hexadecimal:", hex);
Decimal: 123.45
Scientific: 123
Binary: 10
Octal: 15
Hexadecimal: 255

Conclusion

JavaScript's unified number system simplifies numeric operations by treating all numbers as 64-bit floating-point values. This eliminates type conversion issues while supporting integers, decimals, and scientific notation seamlessly.

Updated on: 2026-03-15T23:18:59+05:30

185 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements