Home » JavaScript

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

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





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL







Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.