Loading

Number Object

  • In Javascript to represent the numeric values we use a number object, which may be float or integer value.

  • We can create number objects in javascript by using a number constructor
var n=new Number(value);  

Directly you can assign numbers to variables also:

Example:

<!DOCTYPE html>
<html>

<body>
  <script>
    var x = 102;//integer value  
    var y = 102.7;//floating point value  
    var z = 13e4;//exponent value, output: 130000  
    var n = new Number(10);//integer value by number object  
    document.write(x + " " + y + " " + z + " " + n);
  </script>
</body>

</html>

Output:

102 102.7 130000 10


Number Constants:


Number methods:

  • Numbers in JavaScript are primitive values, but they have methods when accessed as objects (this is known as auto-boxing).
let num = 3.14159;
let str = num.toString();  // Convert number to string
let fixed = num.toFixed(2);  // Round to two decimal places