3

I noticed that functions behave differently on Safari and Chrome, is there any reason?

I don't need a solution, I just would understand why this happens and if it is a bug or whatelse. Here's how you can reproduce it. You'll see different values in Chrome's console and Safari's console.

    const foo = 10;
    {

        const foo = 50;

        function logFoo1(){
            console.log(foo)
        }

        const logFoo2 = function(){
            console.log(foo)
        }

        logFoo1();
        logFoo2();

    }

On safari logFoo1() will prompt 10, on Chrome 50.

Paolo_000
  • 51
  • 9
  • 3
    Somewhat similar to https://stackoverflow.com/questions/57035056/javascript-object-definition-available-before-code-execution-on-safari – Snow Oct 16 '19 at 08:07
  • I think the simple answer, it's a bug with Safari.. bugs happen. To me this seems like a major bug too. – Keith Oct 16 '19 at 08:15
  • @Snow Really interesting, thanks. – Paolo_000 Oct 16 '19 at 08:24
  • @Kaiido As I said, I didn't need a solution because I alredy found the IIFE one. I was just wondering on why. Thanks anyway! – Paolo_000 Oct 16 '19 at 08:24
  • Snow's link apparently has the why (I got fooled by jsfiddle autowrapping in onload handler, thinking it was only in the console, but it's actually only in global scope.) Do you mind if we close your question as a duplicate of the one pointed by Snow? – Kaiido Oct 16 '19 at 08:26
  • 1
    @Kaiido no it's ok. – Paolo_000 Oct 16 '19 at 08:41

1 Answers1

0

Yes It does!

Same piece of Javascript code behaves differently in different browsers. Consider the following piece if code:

<script>

alert('\v supported ' + (String.fromCharCode(11)== '\v'));

</script>

Output:

IE: false

FF: true

Opera: true

Safari: true

The reason behind this is every browser has its own rendering engine.These are examples of browser engines. These engines are nothing but different adaptations of open Web standards

kumaran
  • 366
  • 1
  • 8
  • That isn't the JavaScript the question is asking about. – Quentin Oct 16 '19 at 08:11
  • OK, I did edit :p I really should stay away from Safari questions :p - maybe you should post an answer to the OP to tell him that "browser console doesn't behave" – Bravo Oct 16 '19 at 08:18