What datatype does Safari's do JavaScript actually return?

I send a JavaScript to Safari, something like this:

document.evaluate('some XPath', document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null); items.snapshotLength

It should return a digit, but I expected the digiti to be a string and did a comparison like this:

repeat while ((do JavaScript JSGetAllEntries) is equal to "0")

but this constantly failed, and instead I had to

repeat while ((do JavaScript JSGetAllEntries) is equal to 0)

(no quotation marks around the 0)

Is do JavaScript “smart” and converts digits to integers? Are there other “smart” conversions one should be aware of?

I’m not sure why you expect a string response.

snapshotlength is designed to return ‘the number of nodes in a snapshot result’, therefore is inherently a numeric response.

If you’re ever unsure, you can always coerce manually:

if (do JavaScript JSGetAllEntries) as number = 0...

or

if (do JavaScript JSGetAllEntries) as text = "0"...

Because HTML is inherently string based. do JavaScript’s sibling do shell script returns strings, doesn’t it? Moreover do JavaScript itself accepts a string, and even if you send an integer it is sent as a string.

Because HTML is inherently string based

HTML is, but this is JavaScript, not HTML.

You are asking JavaScript to return the count of entries. Counts are always numbers.

do JavaScript ’s sibling do shell script returns strings

Shell is inherently text-based. You can’t enter numbers in the shell, only textual representations of digits, which the shell and/or executable interprets.

Notably, the do shell script definition specifically identifies its result as text:

FUNCTION SYNTAX

set theResult to do shell script text ¬
as type class ¬
administrator privileges boolean ¬
user name text ¬
password text ¬
with prompt text ¬
altering line endings boolean

RESULT

text the command output

whereas Safari’s do Javascript command shows it can return any data type:

FUNCTION SYNTAX

set theResult to do JavaScript text ¬
in document or tab

RESULT

any

A number or digit is not an integer or float.