Return value from JavaScript to AppleScript

I have exactly the same problem as in this question but I don’t know how to apply that solution to my situation. My JavaScript looks like this:

var result = [];
var xpathResult = document.evaluate('" & xpathExpression & "', document, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
var node;
while (node = xpathResult.iterateNext()) {
    result.push(node);
}
result;

and when I execute it in the browser console it returns a string (or null) but how do I get that string to be returned to my AppleScript?

I have an simpler version of the JavaScript

var result = document.evaluate(''" & xpathExpression & "', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
result.singleNodeValue"

but it has the same problem – no return value. I can even do an alert() and the string is displayed in the browser but it seems impossible to get the string back to the AppleScript.