Built-in functions in JavaScript, a test interface

JavaScript has functions that are available in any script. You can use this interface to try them on different browsers.

Enter a string and click on a function to test, the answer will appear under the button.

Enter an expression:

...

Enter a string:

...

Enter a UTF-8 string:

...


Enter a string:

Enter the radix for an integer:

...


Test functions are used to verify the type or the content of a variable.

isNaN

Test if an object is a number or not.

if(isNaN(x))
{
  alert(x + "is not a number.");
}

isFinite

Test an expression and returns true if the result is a finite number.

if(isFinite(x + 5 * y))
{
  alert("The result is a finite number.");
}

If the value assigned is NaN, the result returned by isFinite will be false.

Encoding functions transform a string such as a file name and its parameters in UTF-8 code that can be used in an URL. Or the opposite.

encodeURI

This function transforms to UTF-8 a string by replacing some characters by codes.

The following codes are not replaced:

/ ? : @ & = + $ , - _ . ! ~ * ' ( ) # - _ . ! ~ * ' ( ) 

Not more than letters and numbers.
Blank spaces are replaced by %20.

encodeURIComponent

The difference with encodeURI, is that some characters that have a function for GET and POST methods are also encoded.

We have a path and filename under a valid operating system, possibly with spaces, commas, and the & + symbols. But these codes are separators in URLs.
To create a URL holding the filename, the following characters must be converted:

 / ? : @ & = + $ , #

decodeURI

This is the inverse function to encodeURI. It recreates an ordinary text from a UTF-8 string.

decodeURIComponent

Inverse function to EncodeURIcomponent, it replaces UTF-8 code by normal characters including special characters whose list is above.

Functions to transform a type of number have a string argument whose content corresponds to a number, or a variable which is assigned a such string or a number.

parseInt

Converts a string into integer. It is possible to choose the radix that we add as optional second parameter: 10 (decimal), 8 (octal), 16 (hexadecimal).
The default radix is 10.

Example:

var x = parseInt("123");

will display 123, but:

var x = parseInt("123", 16)

displays 291.

You can specify a hexadecimal number with the prefix 0x and octal with 0.

var x = parseInt("0xFFFF", 16);    

Without the 16 radix, it returns 0.

parseFloat

Converts a string containing a numeric value in real number. If the string contains a number with decimals, the parseInt function keeps only the integer part, contrary to parseFloat.

Valid values may be in the form 1.2 or 0.5 or 1st-2 or 0.1E +2.

© 2008-2012 Xul.fr