Global Functions

Contact Us or call 1-877-932-8228
Global Functions

Global Functions

JavaScript has a number of global functions. We will examine some of them in this section.

Number(object)

The Number() function takes one argument: an object, which it attempts to convert to a number. If it cannot, it returns NaN, for "Not a Number."

Code Sample:

JavaScriptFunctions/Demos/Number.html
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Number() Function</title>
<link href="style.css" rel="stylesheet">
<script>
	var strNum1 = "1";
	var strNum2 = "2";
	var strSum = strNum1 + strNum2; //returns 12
	alert(strSum);
	
	var intNum1 = Number(strNum1);
	var intNum2 = Number(strNum2);
	var intSum = intNum1 + intNum2; //returns 3
	alert(intSum);
</script>
</head>
<body>
	<p>Nothing to show here.</p>
</body>
</html>

Code Explanation

Because strNum1 and strNum2 are both strings, the + operator concatenates them, resulting in "12".

var strNum1 = "1"; var strNum2 = "2"; var strSum = strNum1 + strNum2; //returns 12 alert(strSum);

After the Number() function has been used to convert the strings to numbers, the + operator performs addition, resulting in 3.

var intNum1 = Number(strNum1); var intNum2 = Number(strNum2); var intSum = intNum1 + intNum2; //returns 3 alert(intSum);

String(object)

The String() function takes one argument: an object, which it converts to a string.

Code Sample:

JavaScriptFunctions/Demos/String.html
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>String() Function</title>
<link href="style.css" rel="stylesheet">
<script>
	var intNum1 = 1;
	var intNum2 = 2;
	var intSum = intNum1 + intNum2; //returns 3
	alert(intSum);
	
	var strNum1 = String(intNum1);
	var strNum2 = String(intNum2);
	var strSum = strNum1 + strNum2; //returns 12
	alert(strSum);
</script>
</head>
<body>
	<p>Nothing to show here.</p>
</body>
</html>

Code Explanation

Because intNum1 and intNum2 are both numbers, the + operator performs addition, resulting in 3.

var intNum1 = 1; var intNum2 = 2; var intSum = intNum1 + intNum2; //returns 3 alert(intSum);

After the String() function has been used to convert the numbers to strings, the + operator performs concatenation, resulting in "12".

var strNum1 = String(intNum1); var strNum2 = String(intNum2); var strSum = strNum1 + strNum2; //returns 12 alert(strSum);

isNaN(object)

The isNaN() function takes one argument: an object. The function checks if the object is not a number (or cannot be converted to a number). It returns true if the object is not a number and false if it is a number.

Code Sample:

JavaScriptFunctions/Demos/isNaN.html
---- C O D E   O M I T T E D ----

<table>
<tr>
	<th>Function</th><th>Result</th>
</tr>
<script>
	document.write("<tr><td>isNaN(4)</td>");
	document.write("<td>" + isNaN(4) + "</td></tr>");
	document.write("<tr><td>isNaN(\"4\")</td>");
	document.write("<td>" + isNaN("4") + "</td></tr>");
	document.write("<tr><td>isNaN(0/0)</td>");
	document.write("<td>" + isNaN(0/0) + "</td></tr>");
	document.write("<tr><td>isNaN(\"hello\")</td>");
	document.write("<td>" + isNaN("hello") + "</td></tr>");
	var strAge = "twelve";
	document.write("<tr><td>isNaN(strAge)</td>");
	document.write("<td>" + isNaN(strAge) + "</td></tr>");
	var intAge = 12;
	document.write("<tr><td>isNaN(intAge)</td>");
	document.write("<td>" + isNaN(intAge) + "</td></tr>");
</script>
</table>
---- C O D E   O M I T T E D ----

Code Explanation

The output will look like this:

parseFloat() and parseInt()

The parseFloat() function takes one argument: a string. If the string begins with a number, the function reads through the string until it finds the end of the number, hacks off the remainder of the string, and returns the result. If the string does not begin with a number, the function returns NaN.

The parseInt() function also takes one argument: a string. If the string begins with an integer, the function reads through the string until it finds the end of the integer, hacks off the remainder of the string, and returns the result. If the string does not begin with an integer, the function returns NaN.

Code Sample:

JavaScriptFunctions/Demos/ParsingNumbers.html
---- C O D E   O M I T T E D ----

<table>
<tr>
	<th>Function</th><th>Result</th>
</tr>
<script>
	var race = "26.2 miles";
	document.write("<tr><td>parseFloat(race)</td>");
	document.write("<td>" + parseFloat(race) + "</td></tr>");
	document.write("<tr><td>parseInt(race)</td>");
	document.write("<td>" + parseInt(race) + "</td></tr>");
	race = "Marathon";
	document.write("<tr><td>parseFloat(race)</td>");
	document.write("<td>" + parseFloat(race) + "</td></tr>");
	document.write("<tr><td>parseInt(race)</td>");
	document.write("<td>" + parseInt(race) + "</td></tr>");
</script>
</table>
---- C O D E   O M I T T E D ----

Code Explanation

The output will look like this:

These "global" functions we have discussed above are actually methods of the window object, but as window is assumed if no object is referenced, we don't need to explicitly write window.parseFloat() or window.parseInt().

Next