Thursday, 28 February 2013

JavaScript isFinite(), isNaN(), parseInt(), parseFloat() and NaN

Description
The isFinite is used to determine whether a specified number is finite or not.isFinite is a top-level function and is not associated with any object.

Syntax

isFinite(number)

Parameters

number : The number to evaluate.

Note that the function returns false if the argument is NAN, positive infinity or negative infinity otherwise it returns true.

Example of isFinite() function

The following example shows how to use isFinite() function.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>JavaScript: isFinite() function.</title>
</head>
<body>
<h1 style="color: red">JavaScript isFinite() function example1</h1>
<hr />
<script type="text/javascript">
//This is done to make the following JavaScript code compatible to XHTML. <![CDATA[
document.write(isFinite("Good Morning")+ "<br />");
document.write(isFinite("2009/01/01")+ "<br />");
document.write(isFinite(455)+ "<br />");
document.write(isFinite(-9.34)+ "<br />");
document.write(isFinite(15-12)+ "<br />");
document.write(isFinite(0)+ "<br />");
//]]>
</script>
</body>
</html>
Please Google+Like this tutorial on FaceBook, Tweet, save it as bookmark andsubscribe with our Feed. Have suggestions? comment using Disqus down this page. Thanks.



JavaScript : isNaN() function



Description

The isNaN function is used to determine whether a value is "NaN" (not a number) or not. isNaN is a top-level function and is not associated with any object.

Syntax

isNan(testvalue)

Parameters

testvalue : The value to evaluate.

Example : isNaN() function

The following example shows how to use isNaN() function.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>JavaScript: isNaN() function.</title>
</head>
<body>
<h1 style="color: red">JavaScript isNaN() function example1</h1>
<hr />
<script type="text/javascript">
//This is done to make the following JavaScript code compatible to XHTML. <![CDATA[
document.write(isNaN("Good Morning")+ "<br />");
document.write(isNaN("2009/01/01")+ "<br />");
document.write(isNaN(455)+ "<br />");
document.write(isNaN(-9.34)+ "<br />");
document.write(isNaN(15-12)+ "<br />");
document.write(isNaN(0)+ "<br />");
//]]>
</script>
</body>
</html>

JavaScript : parseInt() function

Description

The parseInt is used to get a numeric value from a string. parseInt is a top-level function and is not associated with any object.

Syntax

parseInt(numString, radix)

Parameters

numString : Required. A string that represents the value to test.
radix : Optional. An integer that represents the radix of the return value. A value between 2 and 36 indicating the base of the number contained in numString. For example, a radix of ten indicates to convert to a decimal number, eight octal, sixteen hexadecimal, and so on.

Example of parseInt() function

The following example shows how to use parseInt() function.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>JavaScript: parseInt() function.</title>
</head>
<body>
<h1 style="color: red">JavaScript parseInt() function example1</h1>
<hr />
<script type="text/javascript">
//This is done to make the following JavaScript code compatible to XHTML. <![CDATA[
document.write('"21" -> '+parseInt("21") + "<br />");
document.write('"21.42" -> '+parseInt("21.42") + "<br />");
document.write('"100 234 54" -> '+parseInt("100 234 54") + "<br />");
document.write('"89 Math" ->'+parseInt("89 Math") + "<br />");
document.write('"Math 89" ->'+parseInt("Math 89") + "<br />");
document.write("<br />");
document.write(parseInt("100",10)+ "<br />");
document.write(parseInt("001")+ "<br />");
document.write(parseInt("100",8)+ "<br />");
document.write(parseInt("0x10")+ "<br />");
document.write(parseInt("10",16)+ "<br />");
//]]>
</script>
</body>
</html>

JavaScript : parseFloat() function

Description

The parseFloat is used to get a floating value from a string. parseFloat is a top-level function and is not associated with any object.

Syntax

parseFloat(string)

Parameters

string : Required. A string that represents the value to test.

Example of parseFloat() function

The following example demonstrate how to use parseFloat() function.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>JavaScript: parseFloat() function.</title>
</head>
<body>
<h1 style="color: red">JavaScript parseFloat() function example1</h1>
<hr />
<script type="text/javascript">
//This is done to make the following JavaScript code compatible to XHTML. <![CDATA[
document.write('"21" -> '+parseFloat("21") + "<br />");
document.write('"21.42" -> '+parseFloat("21.42") + "<br />");
document.write('"100 234 54" -> '+parseFloat("100 234 54") + "<br />");
document.write('"89 Math" ->'+parseFloat("89 Math") + "<br />");
document.write('"Math 89" ->'+parseFloat("Math 89") + "<br />");
//]]>
</script>
</body>
</html>

JavaScript : NaN

Description

NaN is a value represents not a number.

NaN is a top level property and is not associated with any object.

NaN is never equal to any other number, including NaN itself. It is not possible to check whether a value is number by comparing it with NaN using equal to operator. You have to use the isNaN function instead.

Methods like Number constructor, parseFloat, and parseInt return NaN if the argument passed to them are not numbers.
NaN property can be used to refer an error for a function, provided the function returns a valid number.

Syntax

NaN

Example of JavaScript NAN - when math function fails

<!doctype html>
<head>
<title>Example JavaScript NAN - when math function fails</title>
<meta name='description' content="This example shows that when math function fails, it returns NAN."/>
</head>
<body>
<script>
document.write(Math.abs('MyString'));
</script>
</body>
</html>

Example of JavaScript NAN - when a function tries to read a number fails

<!doctype html>
<head>
<title>Example JavaScript NAN - when a function tries to read a number fails </title>
<meta name='description' content="This example shows that, while trying to parse a number fails, it returns NAN."/>
</head>
<body>
<script>
document.write(parseInt("xyz"));
</script>
</body>
</html>





No comments:

Post a Comment