Home > Software design >  Different `String` function behavior in Chromium and Safari
Different `String` function behavior in Chromium and Safari

Time:05-24

If you run the following code in the browser console, then you get different results in Chromium and Safari.

String(function() { <!--
}).length

Chromium returns:

19

Safari returns:

20

Why?

Note 1

Running via the StackOverflow code editor will deliver the same output, regardless of browser, probably due to the various levels of mediation between the raw output and the browser window.

Note 2

The toString outputs rendered into the console, differ:

Chromium:

'function() { \x3C!--\n}'

Safari:

"function () { <!--
}"

EDIT

This contains a correction of an earlier version of this question that presented incorrect interpretations of the result.

CodePudding user response:

The reason here is not the different linebreak symbols, but in fact Safari adds a space before the () function parentheses in its syntax. Try this to compare the characters side by side more easily:

String(function() {
}).split("")

You will see that in Chrome, it will write it as

function()

whereas in Safari it becomes

function ().

  • Related