Loading...
Loading...
00:00:00

Numbers in Javascript

Number

Numbers in JavaScript are a data type that represents numeric values. They can be integers (whole numbers) or floating-point numbers (decimal numbers). Here are some examples of numbers in JavaScript:

const myInteger = 42;
const myFloat = 3.14;

JavaScript has a built-in object called Number that provides methods and properties for working with numbers. For example, you can use the toFixed method to format a number with a specified number of decimal places:

const myFloat = 3.1415926;
const formattedNumber = myFloat.toFixed(2); // returns "3.14"

You can also use arithmetic operators like +, -, *, /, and % to perform mathematical operations on numbers:

const x = 5;
const y = 2;
const sum = x + y; // returns 7
const difference = x - y; // returns 3
const product = x * y; // returns 10
const quotient = x / y; // returns 2.5
const remainder = x % y; // returns 1

In addition to these basic operations, the Math object provides a number of useful mathematical functions like Math.sqrt, Math.pow, Math.sin, and Math.cos.

In javascript, There is only one type of number. JavaScript Numbers are always use 64bit floating point. This format stores fraction number between 0-51 bits and exponent 52-62 bits and sign 63 bits. Precision of javascript numbers are accurate upto 15 digits. Decimal number is always will be accurate.

let a = 999999999999999;   //15 digits
console.log(a);  //return 999999999999999 accurate
let b = 9999999999999999;   //16 digits
console.log(b);  //return 10000000000000000

Number In Scientific Notations

In javascript scientific notation is very useful for extra large numbers. similarly we can write extra small number using the notation

let a = 15e18;   
console.log(a);  //return 15000000000000000000
var a = 5e-6;   
console.log(a);  //return  0.000005

Number functions and uses

Here are some of the most common number methods in JavaScript:

  1. toString(): Returns a string representation of the number. You can also pass a radix argument to convert the number to a string in a different base (e.g. binary, octal, or hexadecimal).
    const num = 42;
    console.log(num.toString()); // "42"
    console.log(num.toString(2)); // "101010"
    
  2. toFixed(): Formats the number with a fixed number of decimal places and returns a string.
    const num = 3.14159;
    console.log(num.toFixed(2)); // "3.14"
    
  3. toPrecision(): Formats the number to a specified number of significant digits and returns a string.
    const num = 3.14159;
    console.log(num.toPrecision(3)); // "3.14"
    
  4. valueOf(): Returns the primitive value of the number (i.e. the number itself).
    const num = new Number(42);
    console.log(num.valueOf()); // 42
    
  5. isNaN(): Returns true if the value is NaN (not a number).
    console.log(isNaN(42)); // false
    console.log(isNaN("hello")); // true
    
  6. isFinite(): Returns true if the value is a finite number (i.e. not NaN, Infinity, or -Infinity).
    console.log(isFinite(42)); // true
    console.log(isFinite(Infinity)); // false
    

These methods can be useful for formatting, manipulating, and validating numbers in your code. For example, you might use toFixed() to format a currency value for display, or use isNaN() to check if user input is a valid number.

Number Object

Here are all of the functions provided by the JavaScript Number object:

  1. Number(): Converts a value to a number. If the value cannot be converted, it returns NaN.

  2. parseFloat(): Parses a string and returns a floating-point number. If the string cannot be parsed, it returns NaN.

  3. parseInt(): Parses a string and returns an integer. You can also specify a radix argument to convert the string to a different base (e.g. binary, octal, or hexadecimal). If the string cannot be parsed, it returns NaN.

  4. isFinite(): Returns true if the value is a finite number (i.e. not NaN, Infinity, or -Infinity).

  5. isNaN(): Returns true if the value is NaN (not a number).

  6. toExponential(): Returns a string representation of the number in exponential notation.

  7. toFixed(): Formats the number with a fixed number of decimal places and returns a string.

  8. toPrecision(): Formats the number to a specified number of significant digits and returns a string.

  9. toString(): Returns a string representation of the number. You can also pass a radix argument to convert the number to a string in a different base (e.g. binary, octal, or hexadecimal).

  10. valueOf(): Returns the primitive value of the number (i.e. the number itself).

These functions can be used to convert, manipulate, and format numbers in your code. For example, you might use parseInt() to extract a number from a user input string, or use toExponential() to format a very large or very small number for display.

Number Constant 

Here are the constants provided by the JavaScript Number object:

  1. Number.MAX_VALUE: The maximum representable number in JavaScript (approximately 1.79e+308).

  2. Number.MIN_VALUE: The smallest positive representable number in JavaScript (approximately 5e-324).

  3. Number.NaN: A special value that represents "not a number". This value is returned by certain functions when an operation cannot be performed.

  4. Number.NEGATIVE_INFINITY: A special value that represents negative infinity (i.e. a number that is less than any other number, including negative numbers).

  5. Number.POSITIVE_INFINITY: A special value that represents positive infinity (i.e. a number that is greater than any other number, including positive numbers).

These constants can be useful in your code for comparing, validating, and handling special cases involving numbers. For example, you might use Number.MAX_VALUE to set an upper limit on a numeric input field, or use Number.NaN to handle errors when performing mathematical operations.