JavaScript number function | Convert the data type to number

JavaScript number function is used to convert the data type to number. It can Convert different object values (Strings, boolean, etc) to their numbers.

Syntax

Number(object|data type)

Parameter Values

Input variable, But its optional. If no argument is provided, it returns 0.

Return value

It returns the number (integer) format for any type of the JavaScript variable.

Example of JavaScript number function

Let’s see the example of How to convert the data type to number using a Number() function in JS.

<!DOCTYPE html>
<html>
  <head>
    <script>

        var a = "124"; 
		console.log(Number(a))

		var d = new Date("2017-09-30")
		console.log(Number(d))

		var b = true
		console.log(Number(b))

    </script>
  </head>   

</html>

Output:

JavaScript number function

Value cannot be converted

If you pass the value non number then it will return the NaN.

<!DOCTYPE html>
<html>
  <head>
    <script>

        var value = "EyeHunts"
        alert(Number(value))

    </script>
  </head>   

</html>
Convert the data type to number

Do comment if you have any suggestions and doubts on this function.

Note: The All JS Examples codes are tested on the Safari browser (Version 12.0.2) and Chrome.
OS: macOS 10.14 Mojave
Code: HTML 5 Version


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.