×

Index

JavaScript Tutorial

JavaScript Examples

CSS Tutorial

CSS Examples

Number() function with example in JavaScript

JavaScript Number() function: Here, we are going to learn about the Number() function with example in JavaScript.
Submitted by IncludeHelp, on February 02, 2019

JavaScript Number() function

Number() function is a predefined global function in JavaScript, it used to convert an object to the number. If the function is not able to convert the object in a number – it returns "NaN". (Read more: NaN in JavaScript).

Note:

  • If we convert Boolean values to the number, Number() function returns 0 or 1.
  • If we convert a date object to the number, Number() function returns value in milliseconds.

Example

<html>
<head>
<title>JavaScipt Example</title>
</head>

<body>
	<script>		
		var a = "10";	
		var b = "10 20";
		var c = "1234 Hello";
		var d = "Hello 1234";
		var e = true;	
		var f = new Date();
		
		//converting values to number using Number() function
		document.write("Number(a) = " + Number(a) + "<br>");
		document.write("Number(b) = " + Number(b) + "<br>");
		document.write("Number(c) = " + Number(c) + "<br>");
		document.write("Number(d) = " + Number(d) + "<br>");
		document.write("Number(e) = " + Number(e) + "<br>");
		document.write("Number(f) = " + Number(f) + "<br>");
	</script>
</body>
</html>

Output

Number(a) = 10
Number(b) = NaN
Number(c) = NaN
Number(d) = NaN
Number(e) = 1
Number(f) = 1549128507537

JavaScript Built-in Functions »




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.