Quick Contact

    JavaScript Numbers

    Introduction

    JavaScript supports both numeric and floating-point numbers in decimal, hexadecimal, and octal format. Unlike other languages, JavaScript does not distinguish between integer and floating-point numbers. In JavaScript, all integers are represented as floating-point numbers. Here is an example of the numbers in several formats:

    < !DOCTYPE html>
    < html lang="en">
    < head>
    < meta charset="utf-8">
    < title>JavaScript Numbers< /title>
    < /head>
    < body>
    < script>
        // Creating variables
    var x = 5;  // integer number
    var y = 3.27;  // floating-point number
    var z = 0xff;  // hexadecimal number
    
        // Printing variable values
    document.write(x + "< br>");
    document.write(y + "< br>");
    document.write(z);
    < /script>
    < /body>
    < /html>
    

    Output

    5
    3.27
    255
    

    The number is stored in memory in the following manner:

    • Value: 52 bits (0-51)
    • Exponent: 11 bits (52-62)
    • Sign: 1 bit (63)

    Operating on Numbers and Strings

    The + operator, as we may recall from earlier chapters, is used for both addition and concatenation. As a result, executing mathematical operations on integers and strings may provide intriguing outcomes. The following example demonstrates what occurs when we combine numbers and strings:

    Example

    < !DOCTYPE html>
    < html lang="en">
    < head>
    < meta charset="utf-8">
    < title>JavaScript Adding Numbers and Strings< /title>
    < /head>
    < body>
    < script>
        // Creating variables
    var a = 20;
    var b = 30;
    var c = "50";
    
        // Adding a number with a number, the result will be sum of numbers
    document.write(a + b); 
    document.write("< br>");
    
        // Adding a string with a string, the result will be string concatenation
    document.write(c + c); 
    document.write("< br>");
    
        // Adding a number with a string, the result will be string concatenation
    document.write(a + c); 
    document.write("< br>");
    
        // Adding a string with a number, the result will be string concatenation
    document.write(c + a); 
    document.write("< br>");
    
        // Adding strings and numbers, the result will be string concatenation
    document.write("The result is: " + a + b); 
    document.write("< br>");
    
        // Adding numbers and strings, calculation performed from left to right
    document.write(a + b + c); 
    < /script>
    < /body>
    < /html>
    

    Output

    50
    5050
    2050
    5020
    The result is: 2030
    5050
    

    Explanation

    If we thoroughly evaluate the above example, you will see that the result of the last operation is not just a string concatenation, since operators with the same precedence are evaluated from left to right. Because variables a and b are both integers, they are combined first, then the result is concatenated with variable c, which is a string, yielding the final result of 50 + “50” = “5050.”

    Representing Infinity

    Infinity is a number that JavaScript cannot comprehend. In JavaScript, the keywords Infinity and -Infinity are used to express positive and negative infinity, respectively. For example, dividing by 0 yields Infinity, as seen below:

    Example

    < !DOCTYPE html>
    < html lang="en">
    < head>
    < meta charset="utf-8">
    < title>JavaScript Representing Infinity< /title>
    < /head>
    < body>
    < script>
    var a = 8 / 0;
    document.write(x + "< br>"); 
    
    var b = -8 / 0;
    document.write(b); 
    < /script>
    < /body>
    < /html>
    

    Output

    Infinity
    -Infinity
    

    Note:

    Infinity is a special value that represents the mathematical Infinity ∞, which is greater than any number. The typeof operator return number for an Infinity value.

    Performing Operations on Numbers

    To conduct operations on numerical values, JavaScript provides numerous properties and functions. As you may recall from earlier chapters, primitive data types in JavaScript can behave like objects when referred to with the property access notation (i.e. dot notation).

    In the next sections, we will look at the most prevalent number approaches.

    Parsing Integers from Strings

    To parse an integer from a string, use the parseInt() function. This approach is very useful when working with values such as CSS units, such as 50px, 12pt, and so on, and you want to extract the numeric value from it.

    If the parseInt() function detects a non-numeric character in the supplied base, it stops processing and returns the integer value parsed up to that point. The procedure will return NaN if the initial character cannot be transformed into a number (not a number).

    Leading and trailing spaces are allowed. Here’s an example:

    Example

    < !DOCTYPE html>
    < html lang="en">
    < head>
    < meta charset="utf-8">
    < title>JavaScript Parse Integers from Strings< /title>
    < /head>
    < body>
    < script>
    document.write(parseInt("2.19") + "< br>");  
    document.write(parseInt("30px") + "< br>");  
    document.write(parseInt("18pt") + "< br>");  
    document.write(parseInt("0xFF", 16) + "< br>");  
    document.write(parseInt("10 years") + "< br>");  
    document.write(parseInt("Year 2025") + "< br>");  
    document.write(parseInt("12 11 2022"));  
    < /script>
    < /body>
    < /html>
    

    Output

    2
    30
    18
    255
    10
    NaN
    12
    

    Converting Numbers to Strings

    To convert a number to its string counterpart, use the function toString() { [native code] }() function. Optionally, an integer argument in the range 2 through 36 is accepted as determine the base to use for expressing numeric values.

    Formatting Numbers in Exponential Notation

    To format or express a number in exponential notation, use the toExponential() function. Optionally, an integer argument setting the number of digits following the decimal point is accepted by this technique. Furthermore, the returned result is a text rather than a number.

    Formatting Numbers to Fixed Decimals

    When you wish to format a number with a set number of digits to the right of the decimal point, use the toFixed() function. This function returns a string with the precise amount of digits following the decimal point supplied. The digits argument is ignored if it is not given or omitted.

    Formatting Numbers with Precision

    Instead, use the toPrecision() function to get the most suitable form of a number. This function returns a string that represents the integer to the precision requested.

    If the precision is sufficient to contain all of the digits in the integer component of the number, the number is represented in fixed-point notation. The number is otherwise represented in exponential notation. The accuracy parameter is not required.

    Finding the Largest and Smallest Possible Numbers

    There are various characteristics connected with the Number object. The Number object’s Number.MAX VALUE and Number.MIN VALUE fields reflect the greatest and lowest (closest to zero, not most negative) positive values that JavaScript can support. They are constants with the values 1.7976931348623157e+308 and 5e-324, respectively.

    A number that is not inside the range of feasible numbers is represented by the constants Number.POSITIVE INFINITY or Number.NEGATIVE INFINITY.

    FAQS

    Q1. What are JavaScript numbers?

    Number is a simple wrapper object that may be used to represent and manage numbers such as 37 or -9.25. Constants and methods for working with numbers are available in the Number function Object() { [native code] }. The Number() method may be used to convert values of different kinds to numbers.

    Q2. How are numbers represented in JavaScript?

    Numbers in JavaScript are implemented in IEEE 754 double-precision 64-bit binary format (i.e., a number between 21022 and 2+1023, or about 10308 to 10+308, with a numeric precision of 53 bits). Integer numbers up to 253 1 can be precisely expressed.

    Q3. How do you specify an integer in JavaScript?

    The parseInt() function (or method) in JavaScript is used to transform the passed-in string argument or value to an integer value. This method returns an integer with the base provided in the second parameter of the parseInt() function.

    Copyright 1999- Ducat Creative, All rights reserved.

    Anda bisa mendapatkan server slot online resmi dan terpercaya tentu saja di sini. Sebagai salah satu provider yang menyediakan banyak pilihan permainan.