Javascript Global Object - 11 Methods and 3 Properties (Reference)
The global methods and properties can be used with all the JavaScript objects. In browser, the global object is the window object.
Global Functions:
encodeURI()
The encodeURI() method encodes a Uniform Resource Identifier (URI) by replacing each instance of certain characters by one, two, three, or four escape sequences representing the UTF-8 encoding of the character (will always use the shortest escape sequence possible, up to four characters).
var str = "Hello world,/?:@&=+$#";
var x = encodeURI(str);
// x = Hello%20world,/?:@&=+$#
decodeURI()
The decodeURI() method decodes a Uniform Resource Identifier (URI) previously created by encodeURI().
var str = "Hello world,/?:@&=+$#";
var x = encodeURI(str);
// x = Hello%20world,/?:@&=+$#
var y = decodeURI(x);
// y = Hello world,/?:@&=+$#
encodeURIComponent()
The encodeURIComponent() method encodes a Uniform Resource Identifier (URI) component by replacing each instance of certain characters by one, two, three, or four escape sequences representing the UTF-8 encoding of the character (will always use the shortest escape sequence possible, up to four characters).
var str = "Hello world,/?:@&=+$#";
var x = encodeURIComponent(str);
// x = Hello%20world%2C%2F%3F%3A%40%26%3D%2B%24%23
decodeURIComponent()
The decodeURIComponent() method decodes a Uniform Resource Identifier (URI) previously created by encodeURIComponent().
var str = "Hello world,/?:@&=+$#";
var x = encodeURIComponent(str);
// x = Hello%20world%2C%2F%3F%3A%40%26%3D%2B%24%23
var y = encodeURIComponent(x);
// y = Hello world,/?:@&=+$#
eval()
The eval() method evaluates JavaScript code represented as a string.
var x = 10;
var y = 20;
var a = eval("x * y");
// a = 200
// eval(string)
isNaN()
Determines whether a value is an Not-a-Number.
isNaN(123); //false
isNaN(-1.23); //false
isNaN(5 - 2); //false
isNaN(0); //false
isNaN("123"); //false
isNaN("Hello"); //true
isNaN("2005/12/12"); //true
isNaN(""); //false
isNaN(true); //false
isNaN(undefined); //true
isNaN("NaN"); //true
isNaN(NaN); //true
isNaN(0 / 0); //true
isNaN(null); //false
isFinite()
Determines whether a value is a finite number.
isFinite(123); //true
isFinite(-1.23); //true
isFinite(5 - 2); //true
isFinite(0); //true
isFinite("123"); //false
isFinite("Hello"); //false
isFinite("2005/12/12"); //false
isFinite(Infinity); //false
isFinite(-Infinity); //false
isFinite(0 / 0); //false
parseInt()
Parses a string and returns an integer.
parseInt("10"); // 10
parseInt("10.00"); // 10
parseInt("10.33"); // 10
parseInt("34 45 66"); // 34
parseInt(" 60 "); // 60
parseInt("40 years"); // 40
parseInt("He was 40"); // NaN
parseInt("10", 10; // 10
parseInt("010"; // 10
parseInt("10", 8; // 8
parseInt("0x10"; // 16
parseInt("10", 16; // 16
parseFloat()
Parses a string and returns a floating point number.
parseFloat("10"); // 10
parseFloat("10.00"); // 10
parseFloat("10.33"); // 10.33
parseFloat("34 45 66"); // 34
parseFloat(" 60 "); // 60
parseFloat("40 years"); // 40
parseFloat("He was 40"); // NaN
String()
Converts the value of an object to a string.
var x1 = Boolean(0);
var x2 = Boolean(1);
var x3 = new Date();
var x4 = "12345";
var x5 = 12345;
String(x1); // false
String(x2); // true
String(x3); // Sun Oct 11 2020 19:45:10 GMT+0530 (India Standard Time)
String(x4); // 12345
String(x5); // 12345
// String(x1)
Number()
Converts the object argument to a number that represents the object's value.
var x1 = true;
var x2 = false;
var x3 = new Date();
var x4 = "999";
var x5 = "999 888";
Number(x1); // 1
Number(x2); // 0
Number(x3); // 1602425900166
Number(x4); // 999
Number(x5); // NaN
// Number(object)
NaN
The value of NaN is a number that is not a number. It is the only value that is not equal to itself.
var x = Number.NaN;
// Not a number
undefined
In JavaScript, undefined is a special value that is returned by default when a variable has not been assigned a value.
var x = undefined;
// undefined
Infinity
The value of Infinity is a number that is greater than any other number.
var x = Number.NEGATIVE_INFINITY;
// -Infinity
var x = Number.POSITIVE_INFINITY;
// Infinity
