Why are used the Numberobject in JS?

0 votes
24 views
Ravi Vishwakarma asked 8 days ago in Web Application by Ravi Vishwakarma

Why are used the Numberobject in JS?

1 Answer

0 votes
Ethan Karla answered 6 days ago by Ethan Karla
The Number object represents numerical date, either integers or floating-point numbers. In general, you do not need to worry about Number objects because the browser automatically converts number literals to instances of the number class.
Syntax
The syntax for creating a number object is as follows −
var  val-name = new Number(number);
In the place of number, if you provide any non-number argument, then the argument cannot be converted into a number, it returns NaN (Not-a-Number).
Example1:
<!DOCTYPE html>

<html lang='en' xmlns='http://www.w3.org/1999/xhtml'>
<head>
    <meta charset='utf-8' />
    <title></title>
</head>
<body>
    <h2>String </h2>
    <p id='demo' ></p>
    <script>
        document.getElementById('demo').innerHTML =
          Number(true) + '<br>' +
          Number(false) + '<br>' +
          Number('10') + '<br>' +
          Number(' 10') + '<br>' +
          Number('10 ') + '<br>' +
          Number(' 10 ') + '<br>' +
          Number('10.33') + '<br>' +
          Number('10,33') + '<br>' +
          Number('10 33') + '<br>' +
          Number('John');
</script>
</body>
</html>
Example2:
<!DOCTYPE html>

<html>
<body>
<h2>JavaScript Global Functions</h2>
<h2>parseInt() and parseFloat() </h2>
<p>The global JavaScript function parseInt() converts strings to numbers:</p>
<p id='demo'></p>
<script>
document.getElementById('demo').innerHTML =
  parseInt('-10') + '<br>' +
  parseInt('-10.33') + '<br>' +
  parseInt('10') + '<br>' +
  parseFloat('10.33') + '<br>' +
  parseInt('10 6') + '<br>' +
  parseInt('10 years') + '<br>' +
  parseInt('years 10');
</script>
</body>
</html>