/

HTML / CSS / JavaScript Tutorial

JavaScript Global method: Number()

[this page | pdf | back links]

The JavaScript Global Number() method converts an object to a number representing the value of the object (if this is possible) or returns NaN if such a conversion is not possible. If the parameter is a Date object then it returns the number of milliseconds since midnight 1 Jan 1970 (UTC).

 

It has the following syntax with the following parameters:

 

Number(x)

 

Parameter

Required / Optional

Description

x

Optional

Input parameter. If missing then returns 0.

 

EXAMPLE:


HTML USED IN THIS EXAMPLE:
<!DOCTYPE html>
<html> <!-- Copyright (c) Nematrian Limited 2018 -->
<head>
<style>
table,th,tr,td {border: 1px solid black; border-collapse :collapse;}
</style>
</head>
<body>
<table>
<tr>
<th>Example</th>
<th>Resulting value of <code>x</code></th>
</tr>
<tr>
<td><code id="Example"></code></td>
<td><code id="Result"></code></td>
</tr>
</table>

<script>
document.getElementById("Example").innerHTML =
  'var y = "3"; // so y is a string<br>var x = Number(y) * 2;';
var y = "3";
document.getElementById("Result").innerHTML =
  Number(y) * 2;
</script>

</body>
</html>

FUNCTION THAT MAY ASSIST IN TESTING WHETHER FEATURE IS SUPPORTED:
function isSupportedJavaScriptMethodGlobalNumber() {
  return (Number(4) == "4");
}


NAVIGATION LINKS
Contents | Prev | Next | JavaScript DOM (and BOM)


Desktop view | Switch to Mobile