JavaScript Numbers

The Numbers object in JavaScript represents a numerical value, i.e., an integer or a floating-point number.

Let us see how we assign an integer value to a variable:

let a = 25;

Let us see how we assign a floating-point value to a variable:

let a = 25.75;

We can also create a Number object using Number():

let a = new Number(50);

Create a Number object

Let us see an example to create a Number object using Number(). Here, we have:

  • let a = 10: This is a primitive number (typeof is ‘number’).
  • let b = 10.25: This is also a primitive number.
  • let c = new Number(50): This is a Number object (typeof is ‘object’) because it is created using the new keyword

Here is the example:

<!-- JavaScript Numbers -->
<!-- Create a Number object -->
<!DOCTYPE html>
<html>
<head>
<title>Objects in JavaScrip</title> 
</head>
<body>
<h1>Create a Number object</h1>
<p id = "demo"></p>

<script>

let a = 10
let b = 10.25

// Create a Number object c
let c = new Number(50);

document.getElementById("demo").innerHTML = "Sum = "+(a+b+c)

</script>

</body>
</html>

Output

JavaScript Number Object

JavaScript Number Properties

The following are the Number properties:

  • MAX_VALUE
  • MIN_VALUE

MAX_VALUE

The maximum value of a number in JavaScript. The value is 1.7976931348623157E+308. Let us see an example:

<!DOCTYPE html>
<html>
<body>

<h1>JavaScript Numbers</h1>

<p id="test"></p>

<script>
let n = Number.MAX_VALUE;

document.getElementById("test").innerHTML = "Max value = "+n;
</script>

</body>
</html>

Output

JavaScript MAX_VALUE

MIN_VALUE

The minimum value of a number in JavaScript. The value is 5E-324. Let us see an example:

<!DOCTYPE html>
<html>
<body>

<h1>JavaScript Numbers</h1>

<p id="test"></p>

<script>
let n = Number.MIN_VALUE;

document.getElementById("test").innerHTML = "Min value = "+n;
</script>

</body>
</html>

Output

JavaScript MIN_VALUE

JavaScript Number Methods

The following are the Number methods:

  • toString()
  • valueOf()
  • isFinite()
  • isInteger()
  • parseFloat()
  • parseInt()

JavaScript toString() method

The toString() method is used to return the string representation of a number.  Here, we are not converting a number to a string. Let us see an example:

<!DOCTYPE html>
<html>
<body>

<h1>JavaScript Numbers</h1>

<p id="test"></p>

<script>
// Number
let a = 10;  

document.getElementById("test").innerHTML = "String Representation = "+a.toString();
</script>

</body>
</html>

Output

JavaScript toString() method

JavaScript valueOf() method

The valueOf() method is used to return the number’s value. Let us see an example:

<!DOCTYPE html>
<html>
<body>

<h1>JavaScript Numbers</h1>

<p id="test"></p>

<script>
// Number
let n = new Number(5.39726);

document.getElementById("test").innerHTML = "Number's value = "+n.valueOf();
</script>

</body>
</html>

Output

JavaScript valueOf() method

We will get the same output, even if we won’t use valueOf():

document.getElementById(“demo”).innerHTML = “Number’s value = “+a;
Let us understand the difference:
  • valueof() in js

JavaScript isFinite() method

The isFinite() method is used to check whether the value is a finite number. Let us see an example:

<!DOCTYPE html>
<html>
<body>

<h1>JavaScript Numbers</h1>

<p id="test"></p>

<script>
let n = 10;  

document.getElementById("test").innerHTML = "Is it a Finite Number? = "+Number.isFinite(n);
</script>

</body>
</html>

Output

JavaScript isFinite() method

JavaScript isInteger() method

The isInteger() method is used to check whether the value is an integer. Let us see an example:

<!DOCTYPE html>
<html>
<body>

<h1>JavaScript Numbers</h1>

<p id="test"></p>

<script>
let n = 1;  

document.getElementById("test").innerHTML = "Is it an Integer? = "+Number.isInteger(n);
</script>

</body>
</html>

Output

JavaScript isInteger() method

JavaScript parseFloat() method

The parseFloat() method is used to parse a string and convert it to a floating-point number. Let us see an example:

<!DOCTYPE html>
<html>
<body>

<h1>JavaScript Numbers</h1>

<p id="test"></p>

<script>
let n = "12.75Amit";  

document.getElementById("test").innerHTML = "Converted to Float value = "+Number.parseFloat(n);
</script>

</body>
</html>

Output

JavaScript parseFloat() method

JavaScript parseInt() method

The parseInt() method is used to parse a string and convert it to a number. Let us see an example:

<!DOCTYPE html>
<html>
<body>

<h1>JavaScript Numbers</h1>

<p id="test"></p>

<script>
let n = "12.75Amit"  

document.getElementById("test").innerHTML = "Converted to Integer value = "+Number.parseInt(n);
</script>

</body>
</html>

Output

JavaScript parseInt() method

JavaScript Objects
JavaScript Strings
Studyopedia Editorial Staff
contact@studyopedia.com

We work to create programming tutorials for all.

No Comments

Post A Comment