Global functions of JavaScript!!!




Some basic knowledge about JavaScript will help you to write better scripts in Postman for your API testing automation.

We will look about some global functions and their usage. Once you learn you will know when and where to use in your script.

These global functions called globally, rather than on an object directly return their results.

1.String():

This function converts the value of an object to a string.

Eg:

var bool = Boolean(0);

result = String(bool); // false


2. encodeURI():

This function encodes the URI, represents the UTF-8 encoding of the character. It escapes all all characters except:

A-Z a-z 0-9 ; , / ? : @ & = + $ – _ . ! ~ * ‘ ( ) #

The decodeURI() function to decode an encoded URI.

Eg:

const uri = β€˜https://test.com/?x=ΡˆΠ΅Π»Π»Ρ‹’;

const encoded = encodeURI(uri);

console.log(encoded); // https://test.com/?x=%D1%88%D0%B5%D0%BB%D0%BB%D1%8B

const decoded = decodeURI(uri);

console.log(decoded); // https://test.com/?x=ΡˆΠ΅Π»Π»Ρ‹


3. encodeURIComponent():

This function encodes a URI component including the special characters.

The decodeURIComponent() function to decode an encoded URI component.

Eg:

const uri = β€˜#^$&%&^’;

const encodedComp = encodeURIComponent(uri);

console.log(encodedComp); // %23%5E%24%26%25%26%5E

const decodedComp = decodeURIComponent(uri);

console.log(decodedComp); // #^$&%&^


4. isFinite():

This function tell if the number passed is a finite number or not and returns false if the value is +infinity, -infinity, or NaN, otherwise it returns true.

var text= isFinite(β€œTest”); // false

var eg = isFinite(910); // true

var eg = isFinite(null); // true, would’ve been false with the

// more robust Number.isFinite(null)


5. Number():

This method converts the variable into numbers.

Eg:

var num1 = Number(true);

console.log(num1); // 1

var num2 = Number(β€œ123”);

console.log(num2); // 123


6. parseInt():

This function parses a string and returns an integer. Only the first number in the string is returned. If the first character cannot be converted to a number, parseInt() returns NaN.

Eg:

var int1= parseInt(β€œ12.67”);

console.log(int1); // 12

var int2= parseInt(β€œ45 90 12”);

console.log(int2); // 45


7.parseFloat():

This function parses a string and returns a floating point number.

This function determines if the first character in the specified string is a number. If it is, it parses the string until it reaches the end of the number, and returns the number as a number, not as a string.

Eg:

var float1= parseFloat(β€œ12.67”);

console.log(float1); // 12.67

var float2= parseFloat(β€œ45 90 12”);

console.log(float2); // 45


8. isNan():

This function determines whether a value is NaN (Not-A-Number) or not.

Eg:

var nan1= isNan(β€œ3435”);

console.log(nan1); // false

var nan2= isNan(β€œtest”);

console.log(nan2); // true


9. eval():

This function evaluates JavaScript code represented as a string.

The argument of the eval() function is a string. If the argument of eval() is not a string, eval() returns the argument unchanged.

var a = 5;

var b= 2;

var res= eval(β€œx * y”);

console,log(res); // 10

I found these are interesting and much useful!! Please let me know your thoughts…


Leave a comment