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β¦

