You are here: Reference > JavaScript > core > global properties and methods

# Global properties and methods

This section lists all JavaScript properties and methods that can be used from anywhere in a script.
These items apply to no particular objects.

### Properties:

Property Support Description
Infinity Same as the Number.POSITIVE_INFINITY property. There are two infinity types in JavaScript: Number.POSITIVE_INFINITY (Infinity) and Number.NEGATIVE_INFINITY (-Infinity). The value of a number is Infinity if it is greater than the Number.MAX_VALUE constant (a bit less than 2^1024). The value of a number is -Infinity if it is less than the -1 * Number.MAX_VALUE constant (a bit more than -1 * 2^1024).
The Infinity property behaves like the infinity in Mathematics.
• Any number added to Infinity is also Infinity.
• Any number added to -Infinity is also -Infinity.
• Any number except 0 multiplied by Infinity is also Infinity.
• Any number except 0 multiplied by -Infinity is also -Infinity.
• Infinity is greater than any number, while -Infinity is less than any number.
You can use the isFinite global method to check the finiteness of a number. Note: it does not mean that JavaScript can work with numbers between (-1 * Number.MAX_VALUE, Number.MAX_VALUE) correctly. In a 32-bit system, the real limit is (-1 * 2^53, 2^53). See Example 1 for details.
NaN Same as the Number.NaN property. The NaN property is a constant value that indicates that the value of an expression is 'Not-A-Number'. You can use the isNaN global method to check if a value is NaN. If a method (such as parseFloat, parseInt) returns NaN, it means that the specified parameter cannot be parsed as a number. The following expressions also return NaN: 0/0, Infinity - Infinity, Infinity * 0, Infinity / Infinity. See Example 1 for details.
undefined The undefined property can be used to check if a variable is not declared, or it does not have a value assigned to it. See the Example 2 for details.

### Methods:

Method Support Description
decodeURI (encodedUri) Returns the unencoded version of a string encoded with the encodeURI method.
 encodedUri Required. The encoded Uniform Resource Identifier.
decodeURIComponent (encodedURIString) Returns the unencoded version of a string encoded with the encodeURIComponent method.
 encodedURIString Required. The encoded Uniform Resource Identifier component.
encodeURI (URIString) Encodes any string to a valid Uniform Resource Identifier (URI).
Encoding is necessary if you want to use a string as a URI, such as a Web page address. Characters - other than Latin alphanumeric characters and the following ones: !#\$&'()*+,-./:;=?@_ - are changed to their hexadecimal representations (%XX, where XX is the hexadecimal character code of the character, such as %20 is a space) during the conversion.

Note that the encodeURIComponent method is similar to the encodeURI method, the only difference is that the encodeURIComponent method also encodes the #\$&+,/:;=?@ characters.
If a string contains a complete URL and you need to convert it to a valid URI, use the encodeURI method. If you want to create the query part of an URL or the body of an HTTP request, use the encodeURIComponent method on the components. The encodeURIComponent method is most often used for AJAX form submit.

If you need to decode a string encoded with the encodeURI method, use the decodeURI method.
 URIString Required. Specifies a complete Uniform Resource Identifier.
encodeURIComponent (str) Encodes a string to a valid Uniform Resource Identifier (URI) component.
Encoding is necessary if you want to use a string as a component of a query part of an URL. Characters - other than Latin alphanumeric characters and the following ones: !'()*-._ - are changed to their hexadecimal representations (%XX, where XX is the hexadecimal character code of the character, such as %20 is a space) during the conversion, so that it can be passed from one place to another like form data.

Note that the encodeURI method is similar to the encodeURIComponent method, the only difference is that the encodeURI method does not encodes the #\$&+,/:;=?@ characters.
If a string contains a complete URL and you need to convert it to a valid URI, use the encodeURI method. If you want to create the query part of an URL or the body of an HTTP request, use the encodeURIComponent method on the components. The encodeURIComponent method is most often used for AJAX form submit. See Example 5 and 6 for details.

If you need to decode a string encoded with the encodeURIComponent method, use the decodeURIComponent method.
 str Required. Specifies an arbitrary text.
escape (str) Deprecated. Use the encodeURI or the encodeURIComponent method instead.

Returns the given string in Unicode format. All characters are replaced by their hexadecimal representations (%XX, where XX is the hexadecimal character code of the character, such as %20 is a space), except Latin alphanumeric characters and the following ones: *+-./@_. Since the escape method does not handle non-ASCII characters correctly, avoid the use of it.

If you need to decode a string encoded with the escape method, use the unescape method.
 str Required. Specifies the string to encode.
eval (codeString) Evaluates and executes the given string of JavaScript code.

Try to avoid the use of the eval method!
The code passed to eval is interpreted in the context of the call to eval so the surrounding code must be interpreted by the browser at runtime.
In most cases, eval is unnecessary, but if there is no other solution, use the Function object instead, it is much more faster than eval, because it executes the code within the function and the JavaScript compiler can optimise the surrounding context. See Example 7 - 10 for details.
 codeString Required. String that specifies the code to execute.
isFinite (number) Returns a Boolean value that indicates whether the given number is finite (negative or positive), or not.
 number Required. Specifies the numeric value to check.
isNaN ([numValue]) Returns a Boolean value that indicates whether the given value is a number, or not. If it returns 'true' the value is 'Not a Number'.
 numValue Optional. Numeric vale.
parseFloat (numString) Converts the given string to a floating point number. The base is always decimal. The parseFloat method converts the longest prefix of the specified string to a number that can be successfully parsed. Leading whitespaces are allowed, exponential notations are supported. If the given string is empty or its first non-whitespace character is not a digit, a decimal point or a plus or minus sign, the parseFloat method returns NaN.
 numString Required. The string to convert.
Note that the Number object provides similar functionality.
parseInt (numString[, radix]) Converts the given string of the specified base to an integer. The parseInt method converts the longest prefix of the specified string to an integer that can be successfully parsed. Leading whitespaces are allowed. If the given string is empty or its first non-whitespace character is not a digit or a plus or minus sign, the parseInt method returns NaN.
Be careful about the parseInt method! If the radix parameter is not specified, the parseInt method automatically detects the base. If the string starts with "0x" or "0X", the base is hexadecimal. If the string starts with "0" and the next character is neither "x" nor "X", the base is octal. In other cases, the base is decimal.
Also see Example 3 and 4 below.
If you need to convert a number to a string that represents the number in the specified base, use the toString method of the Number object.
 numString Required. The string to convert. radix Optional. Specifies the binary (2), octal (8), decimal (10), hexadecimal (16) or other (2-36) base, from which the given string should be converted to a decimal integer. If not specified, the parseInt method automatically detects the base. The maximum base is 36. The numerals in base of 36 are: 0-9 and a-z.
Note that the Number object and the parseFloat method together with the ceil, floor and round methods provide similar functionality.
unescape (encodedStr) Deprecated. Use the decodeURI or the decodeURIComponent method instead.
Decodes a string encoded with the escape method.
 encodedStr Required. Specifies the string to decode.

## Examples:

#### Example 1

Working with numbers in JavaScript:
```        // create Infinity from numbers
document.write ("1 / 0 = ");
document.write (1 / 0);     // output: Infinity
document.write ("<br />");

document.write ("-1 / 0 = ");
document.write (-1 / 0);    // output: -Infinity
document.write ("<br />");

document.write ("0 / 0 = ");
document.write (0 / 0);     // output: NaN
document.write ("<br />");

document.write ("2^1024 = ");
document.write (Math.pow (2, 1024));    // output: Infinity
document.write ("<br />");

document.write ("2^1023 = ");
document.write (Math.pow (2, 1023));    // output: 8.98846567431158e+307
document.write ("<br />");

// the real limit for integers
var num1 = Math.pow (2, 53) - 1;
var num2 = Math.pow (2, 53);
var num3 = Math.pow (2, 53) + 1;
document.write ("2^53 - (2^53 - 1) = ");
document.write (num2 -num1);    // output: 1
document.write ("<br />");
document.write ("(2^53 + 1) - 2^53 = ");
document.write (num3 -num2);    // output: 0
document.write ("<br />");

var num1 = -1 * Math.pow (2, 53) - 1;
var num2 = -1 * Math.pow (2, 53);
var num3 = -1 * Math.pow (2, 53) + 1;
document.write ("(-1 * 2^53) - (-1 * 2^53 - 1) = ");
document.write (num2 -num1);    // output: 0
document.write ("<br />");
document.write ("(-1 * 2^53 + 1) - (-1 * 2^53) = ");
document.write (num3 -num2);    // output: 1
document.write ("<br />");

// addition, subtraction operations for Infinity
document.write ("Infinity + 1 = ");
document.write (Infinity + 1);  // output: Infinity
document.write ("<br />");

document.write ("Infinity - 1 = ");
document.write (Infinity - 1);  // output: Infinity
document.write ("<br />");

document.write ("-Infinity + 1 = ");
document.write (-Infinity + 1);     // output: -Infinity
document.write ("<br />");

document.write ("-Infinity - 1 = ");
document.write (-Infinity - 1);     // output: -Infinity
document.write ("<br />");

document.write ("Infinity + Infinity = ");
document.write (Infinity + Infinity);   // output: Infinity
document.write ("<br />");

document.write ("Infinity + -Infinity = ");
document.write (Infinity + -Infinity);  // output: NaN
document.write ("<br />");

document.write ("Infinity - Infinity = ");
document.write (Infinity - Infinity);   // output: NaN
document.write ("<br />");

document.write ("Infinity - -Infinity = ");
document.write (Infinity - -Infinity);  // output: Infinity
document.write ("<br />");

// multiplication operations for Infinity
document.write ("Infinity * 1 = ");
document.write (Infinity * 1);  // output: Infinity
document.write ("<br />");

document.write ("Infinity * -1 = ");
document.write (Infinity * -1);     // output: -Infinity
document.write ("<br />");

document.write ("Infinity * 0 = ");
document.write (Infinity * 0);  // output: NaN
document.write ("<br />");

document.write ("Infinity * Infinity = ");
document.write (Infinity * Infinity);   // output: Infinity
document.write ("<br />");

document.write ("Infinity * -Infinity = ");
document.write (Infinity * -Infinity);  // output: -Infinity
document.write ("<br />");

// division operations for Infinity
document.write ("Infinity / 1 = ");
document.write (Infinity / 1);  // output: Infinity
document.write ("<br />");

document.write ("Infinity / -1 = ");
document.write (Infinity / -1);     // output: -Infinity
document.write ("<br />");

document.write ("Infinity / 0 = ");
document.write (Infinity / 0);  // output: Infinity
document.write ("<br />");

document.write ("Infinity / Infinity = ");
document.write (Infinity / Infinity);   // output: NaN
document.write ("<br />");

document.write ("Infinity / -Infinity = ");
document.write (Infinity / -Infinity);  // output: NaN
document.write ("<br />");```
Did you find this example helpful?

#### Example 2

How to check the undefined state of a variable:
```    // use the undefined global property for checking
if (x === undefined)
document.write ("x is undefined <br />");
else
document.write ("x is defined <br />");
// use the typeof operator for checking
if (typeof x == 'undefined')
document.write ("x is undefined <br />");
else
document.write ("x is defined <br />");

var x;  // declare x without a value

// use the undefined global property for checking
if (x === undefined)
document.write ("x is undefined <br />");
else
document.write ("x is defined <br />");
// use the typeof operator for checking
if (typeof x == 'undefined')
document.write ("x is undefined <br />");
else
document.write ("x is defined <br />");

x = 2;  // x has a value

// use the undefined global property for checking
if (x === undefined)
document.write ("x is undefined <br />");
else
document.write ("x is defined <br />");
// use the typeof operator for checking
if (typeof x == 'undefined')
document.write ("x is undefined <br />");
else
document.write ("x is defined <br />");```
Did you find this example helpful?

#### Example 3

The parseInt examples all return 10:
```parseInt ("1010", 2);
parseInt ("12", 8);
parseInt ("012");
parseInt ("10", 10);
parseInt ("10");
parseInt ("10.22");
parseInt ("A", 16);
parseInt ("0xA");
parseInt ("10ABC");
parseInt ("012ABC");
parseInt ("0xArt");```
Did you find this example helpful?

#### Example 4

If the second parameter (radix) is not specified, the parseInt method automatically detects the base. In some cases, it returns unexpected result (for details, see the parseInt method):
```    // hexadecimal
parseInt ("10");    // return 10
// octal
parseInt ("010");   // return 8
// octal, 8 is not a valid octal digit
parseInt ("08");    // return 0
// octal, 9 is not a valid octal digit
parseInt ("09");    // return 0```
Did you find this example helpful?

#### Example 5

This example uses the encodeURIComponent method to encode form data before submitting. This example and the next one use AJAX to submit a form without reloading the page. The two examples are similar, but the first one uses synchronous while the second one uses asynchronous data transfer. In synchronous mode, the send method of the XMLHttpRequest object waits for a reply, so the user cannot interact with the browser until the response has completed. In asynchronous mode, the send method does not wait for a reply but instead returns immediately, so it allows you to perform other operations while a time-consuming task is being performed in the background.
 Code ajax_form.js register.php
```<head>
<script type="text/javascript" src="ajax_form.js"></script>
<style>
.error {
display: none;
color: #a00000;
font-weight: bold;
}
</style>
<script type="text/javascript">
function HideAllErrorFields () {
for (var i = 1; i <= 4; i++) {
var field = document.getElementById ("error" + i);
field.style.display = "none";
}
}

function ShowErrorFields (idsStr) {
var ids = idsStr.split (",");
for (var i = 0; i < ids.length; i++) {
var field = document.getElementById ("error" + ids[i]);
if (field) {
field.style.display = "block";
}
}
}

function AjaxSend (form, url, method) {
// hide all error fields
HideAllErrorFields ();

// get message data
var data = GetMessageBody (form);   // defined in ajax_form.js

// send the request
var httpRequest = CreateRequestObj ();  // defined in ajax_form.js
// try..catch is required if working offline
try {
httpRequest.open (method, url, false);  // synchron
httpRequest.send (data);
}
catch (e) {
alert ("Cannot connect to the server!");
return;
}

// handle the response
if (IsRequestSuccessful (httpRequest)) {    // defined in ajax_form.js
if (httpRequest.responseText === "ok") {    // registration is successful
/*
// if redirection is required
location.href = "/index.php";
*/
}
else {  // some fields are invalid
ShowErrorFields (httpRequest.responseText);
}
}
else {
}
}
</script>
<body>
This is a sample registration form.
The username must be between 2 and 20 characters, the password must be between 6 and 20 characters.
A user named Dottoro is already registered.
<br />
Try to register both valid and invalid values!
<br /><br />
<form onsubmit="AjaxSend (this, 'register.php', 'post'); return false;">
User Name: <input type="text" name="userName" />
<div class="error" id="error1">Must be between 2 and 20 characters.</div>
<div class="error" id="error2">A user already exists with the same name.</div>
<br /><br />
<div class="error" id="error3">Must be between 6 and 20 characters.</div>
<br /><br />
<div class="error" id="error4">Must be the same as the password.</div>
<br /><br />
<input type="submit" value="Register" />
</form>
</body>```
Did you find this example helpful?

#### Example 6

This example is similar to the previous one, but it uses asynchronous data transfer and displays blinking text while waiting for the server response (the register_delay.php script waits two seconds before responding).
 Code ajax_form.js register_delay.php
```<head>
<script type="text/javascript" src="ajax_form.js"></script>
<style>
.error {
display: none;
color: #a00000;
font-weight: bold;
}
#progress {
visibility: hidden;
color: #00a000;
}
</style>
<script type="text/javascript">
function HideAllErrorFields () {
for (var i = 1; i <= 4; i++) {
var field = document.getElementById ("error" + i);
field.style.display = "none";
}
}

function ShowErrorFields (idsStr) {
var ids = idsStr.split (",");
for (var i = 0; i < ids.length; i++) {
var field = document.getElementById ("error" + ids[i]);
if (field) {
field.style.display = "block";
}
}
}

var registering = false;

registering = false;
StopAnim ();

// prevent memory leaks

if (IsRequestSuccessful (httpRequest)) {    // defined in ajax_form.js
if (httpRequest.responseText === "ok") {    // registration is successful
/*
// if redirection is required
location.href = "/index.php";
*/
}
else {  // some fields are invalid
ShowErrorFields (httpRequest.responseText);
}
}
else {
}
}
}

function AjaxSend (form, url, method) {
// avoid resend data while registering
if (registering) {
return;
}
// hide all error fields
HideAllErrorFields ();

// get message data
var data = GetMessageBody (form);   // defined in ajax_form.js

// send the request
var httpRequest = CreateRequestObj ();  // defined in ajax_form.js
// try..catch is required if working offline
try {
httpRequest.open (method, url, true);   // asynchron
httpRequest.send (data);
}
catch (e) {
alert ("Cannot connect to the server!");
return;
}

registering = true;
StartAnim ();
}

var animTimerId = 0;
function StartAnim () {
var progress = document.getElementById ("progress");
progress.style.visibility = "visible";
animTimerId = setInterval (RegAnim, 100);
}
function RegAnim () {
var progress = document.getElementById ("progress");
progress.style.visibility = (progress.style.visibility == "visible") ? "hidden" : "visible";
}
function StopAnim () {
var progress = document.getElementById ("progress");
progress.style.visibility = "hidden";
clearInterval (animTimerId);
}

</script>
<body>
This is a sample registration form.
The username must be between 2 and 20 characters, the password must be between 6 and 20 characters.
A user named Dottoro is already registered.
<br />
Try to register both valid and invalid values!
<br /><br />
<form onsubmit="AjaxSend (this, 'register_delay.php', 'post'); return false;">
User Name: <input type="text" name="userName" />
<div class="error" id="error1">Must be between 2 and 20 characters.</div>
<div class="error" id="error2">A user already exists with the same name.</div>
<br /><br />
<div class="error" id="error3">Must be between 6 and 20 characters.</div>
<br /><br />
<div class="error" id="error4">Must be the same as the password.</div>
<br /><br />
<input type="submit" value="Register" />
<div id="progress">Registering</div>
</form>
</body>```
Did you find this example helpful?

#### Example 7

Example 7 and 8 show how to avoid the use of the eval method to improve performance.
```var str0 = "text0";
var str1 = "text1";
var str2 = "text2";

function GetStr (i) {
return eval ("str" + i);
}

document.write (GetStr (0));    // text0
document.write (GetStr (2));    // text2```
Did you find this example helpful?

#### Example 8

Since global variables are members of the window object, the previous example can also be implemented like this:
```var str0 = "text0";
var str1 = "text1";
var str2 = "text2";

function GetStr (i) {
return window["str" + i];
}

document.write (GetStr (0));    // text0
document.write (GetStr (2));    // text2```
Did you find this example helpful?

#### Example 9

In some cases, a code snippet needs to be evaluated at runtime. For example, when the response of an AJAX request contains some source code that needs to be executed by the JavaScript interpreter. In that case, we can use both the eval method and the Function object to solve the problem. The prefered solution is to use the Function object, because it is much more faster than the eval method (see eval for details).
This example uses the eval method:
```var response = "2+3";
var result = eval (response);
document.write (result);    // 5```
Did you find this example helpful?

#### Example 10

This example uses the Function object to implement the same functionality as the previous example:
```var response = "2+3";
var result = (new Function ("return " + response)) ();
document.write (result);    // 5```
Did you find this example helpful?