Oct 15, 2020

API Testing 'A Beginners View': JavaScript pop() Method

The pop() method removes the last element from an array and returns the removed value.

If you call pop() to an empty array, it will return output as 'undefined'.

Syntax
arrName.pop()

Return value
The removed element from the array and it would be 'undefined' if the array is empty.

Explanation with Example:
Response Body:









Test Script:
var resp = JSON.parse(responseBody);

//API Testing 'A Beginners View': JavaScript pop() Method
accept_encoding = [];
var split = "gzip, deflate, br";
var split_value = split.split(",");
console.log("accept-encoding is: " + split_value);
var pop_value = split_value.pop();
console.log(pop_value);
var pop_value = split_value.pop();
console.log(pop_value);
var pop_value = split_value.pop();
console.log(pop_value);
var pop_value = split_value.pop();
console.log(pop_value);
var pop_value = split_value.pop();
console.log(pop_value);
Console Output:





In the above example, I have used the split() method from a previous post on the split() method (https://softwaretestingcafebyjency.blogspot.com/2020/10/JavaScript-split-Method.html) and continued to add the pop() method to return the last element for the same example.

The split_value.pop() method is used to return the last element from the string defined.


No comments: