JavaScript Introduction | Functions | Predefined Global Functions

Predefined Functions

JavaScript predefines and provides several global functions with various features for user convenience.
These global functions can be used directly from any type of JavaScript object.

The predefined global functions in JavaScript are as follows.

  1. eval()
  2. isFinite()
  3. isNaN()
  4. parseFloat()
  5. parseInt()
  6. decodeURI()
  7. decodeURIComponent()
  8. encodeURI()
  9. encodeURIComponent()
  10. escape()
  11. unescape()
  12. Number()
  13. String()

eval()

The eval() function executes JavaScript code represented as a string.

Syntax

eval("string");
var x = 10, y = 20;
var a = eval("x + y"); // 30
var b = eval("y * 3"); // 60
document.write(a + "<br>" + b);

isFinite()

The isFinite() function checks whether the passed value is a finite number and returns the result. If the value passed as an argument is not a number, it is converted to a number and checked.

Syntax

isFinite(valueToCheck);
isFinite(123);       // true
isFinite(123e100);   // true
isFinite(0);         // true
isFinite(true);      // true
isFinite(false);     // true
isFinite(null);      // true
isFinite("123");     // true
isFinite("");        // true

isFinite("string");  // false
isFinite(undefined); // false
isFinite(NaN);       // false

isNaN()

The isNaN() function checks whether the passed value is NaN and returns the result.
If the value passed as an argument is not a number, it is forcibly converted to a number and checked.

You can also use the typeof operator instead to check whether the passed value is a number.

Syntax

isNaN(valueToCheck);
isNaN(123);       // false
isNaN(123e100);   // false
isNaN(0);         // false
isNaN(true);      // false
isNaN(false);     // false
isNaN(null);      // false
isNaN("123");     // false
isNaN("");        // false

isNaN("string");  // true
isNaN(undefined); // true
isNaN(NaN);       // true

Because this function can produce unexpected results due to forced conversion to numbers, ECMAScript 6 and later recommend using the Number.isNaN() method.

parseFloat()

The parseFloat() function parses a string and returns it as a floating-point number.

Syntax

parseFloat("string");
parseFloat("123");        // 123
parseFloat("123.000");    // 123
parseFloat("123.456");    // 123.456
parseFloat("12 34 56");   // 12
parseFloat(" 123 ");      // 123
parseFloat("123 chocolate"); // 123
parseFloat("chocolate 123"); // NaN

parseInt()

The parseInt() function parses a string and returns it as an integer.

Syntax

parseInt("string");
parseInt("123");        // 123
parseInt("123.000");    // 123
parseInt("123.456");    // 123
parseInt("12 34 56");   // 12
parseInt(" 123 ");      // 123
parseInt("123 chocolate"); // 123
parseInt("chocolate 123"); // NaN

parseInt("10", 10);     // 10
parseInt("10", 8);      // 8
parseInt("10", 16);     // 16
parseInt("0x10");       // 16

As in the example above, if a specific radix is passed as the second argument to parseInt(), the function returns an integer according to that radix.
Also, if the passed string starts with “0x”, parseInt() recognizes that string as hexadecimal.

Previously, parseInt() recognized a passed string that started with “0” as octal.
However, JavaScript no longer supports this syntax.

encodeURI() and encodeURIComponent()

The encodeURI() function encodes all characters as escape sequences except special characters that represent addresses in a URI.
However, encodeURIComponent() also escape-sequence encodes all characters in the URI that encodeURI() does not encode.

Syntax

encodeURI(URIToEncode);
encodeURIComponent(URIToEncode);
var uri = "http://google.com/search.php?name=HongGilDong&city=Seoul";

var enc1 = encodeURI(uri);
var enc2 = encodeURIComponent(uri);
document.write(enc1 + "<br>" + enc2);

decodeURI() and decodeURIComponent()

The decodeURI() function decodes a URI created by encodeURI() or another method. The decodeURIComponent() function decodes a URI component created by encodeURIComponent() or another method.

Syntax

decodeURI(URIToDecode);
decodeURIComponent(URIToDecode);
var uri = "http://google.com/search.php?name=HongGilDong&city=Seoul";

var enc1 = encodeURI(uri);
var enc2 = encodeURIComponent(uri);
document.write(enc1 + "<br>" + enc2 + "<br><br>");

var dec1 = decodeURI(enc1);
var dec2 = decodeURIComponent(enc2);
document.write(dec1 + "<br>" + dec2);

escape() and unescape()

The escape() function converts specific characters in the passed string to hexadecimal escape sequence characters.
The unescape() function converts hexadecimal escape sequence characters created by escape() or another method in the passed string back to the original characters.

Syntax

escape("stringToConvert");
unescape("stringToRestore");
var str = "Hello! World ?#$";

var esc = escape(str);
var une = unescape(esc);
document.write(esc + "<br>" + une);

The escape() function has not been supported since JavaScript 1.5, so use encodeURI() or encodeURIComponent() instead.
The unescape() function has not been supported since JavaScript 1.5, so use decodeURI() or decodeURIComponent() instead.

Number()

The Number() function returns the value of the passed object as a number.

Syntax

Number(object);
Number("123");        // 123
Number("123.000");    // 123
Number("123.456");    // 123.456
Number("12 34 56");   // NaN
Number("123 chocolate"); // NaN

Number(true);         // 1
Number(false);        // 0
Number(new Date());   // returns the number corresponding to the current date.
Number(null);         // 0

String()

The String() function returns the value of the passed object as a string.

Syntax

String(object);
String(123);        // 123
String(123.456);    // 123.456
String("123");      // 123
String(new Date()); // returns the string corresponding to the current date.
String(null);       // null

String(true);       // true
String(false);      // false