Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Different `String` function behavior in Chromium and Safari

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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 ().

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading