Why these syntactically identical functions produce different results?

A.V.D. Kishore
function foo1() {
    return {
        bar: "hello"
    };
}

function foo2() {
    return
    {
        bar: "hello"
    };
}

console.log(foo1());
console.log(foo2());

Can I get an explanation why the two functions print out different results even though the code looks the same?

thefourtheye

Automatic semicolon insertion

Quoting the specification,

When a continue, break, return, throw, or yield token is encountered and a LineTerminator is encountered before the next token, a semicolon is automatically inserted after the continue, break, return, throw, or yield token.

So the code will become like this

function foo2() {
    return;          // Note the `;` after `return`
    {
        bar: "hello"
    };
}

The return statement terminates and then there is an object after that, which is basically unreachable code. Since the return statement doesn't return anything explicitly, undefined will be returned.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why these syntactically identical functions produce different results?

From Dev

Why do two seemingly identical variables produce two different results?

From Dev

Why these assignments produce different results?

From Dev

jQuery and JS equivalent functions produce different results

From Dev

Why does Python produce different sets from identical input?

From Dev

Why do Python generator functions not have a syntactically different notation from 'regular' functions?

From Dev

Why does different scrypt implementation produce different results?

From Dev

Why does an operation on two identical lists end up in different results?

From Dev

Why does LINQ GroupBy produce different results when preceded by ToArray()?

From Dev

Why do ng-bind-html and $sanitize produce different results?

From Dev

Why do these two python regexes produce different results?

From Dev

Why does gzip on tar output always produce different results?

From Dev

Why does LINQ GroupBy produce different results when preceded by ToArray()?

From Dev

SQL In and having-clause, why do they produce different results?

From Dev

Why do ng-bind-html and $sanitize produce different results?

From Dev

Why does default sort produce different results with same line beginnings?

From Dev

Why does "wsl" produce different results when using quotes?

From Dev

Why does GridSearchCV on an SVC in scikit-learn produce different probabilities given identical inputs?

From Dev

Why does GridSearchCV on an SVC in scikit-learn produce different probabilities given identical inputs?

From Dev

Different results from Identical databases

From Dev

atof and stringstream produce different results

From Dev

Why is the planner coming up with different results for functions with different volatilities?

From Dev

Why do wkhtmltopdf.exe produce different results on different Windows machines?

From Dev

Why does using std::remove_reference and std::remove_const in different order produce different results?

From Dev

Why do wkhtmltopdf.exe produce different results on different Windows machines?

From Dev

Why are Python decorators with arguments syntactically different from those without?

From Dev

Why do these two golang integer conversion functions give different results?

From Dev

Why do these two python functions return different results?

From Dev

Why does using Array.map(parseInt) on an array of strings produce different results

Related Related

  1. 1

    Why these syntactically identical functions produce different results?

  2. 2

    Why do two seemingly identical variables produce two different results?

  3. 3

    Why these assignments produce different results?

  4. 4

    jQuery and JS equivalent functions produce different results

  5. 5

    Why does Python produce different sets from identical input?

  6. 6

    Why do Python generator functions not have a syntactically different notation from 'regular' functions?

  7. 7

    Why does different scrypt implementation produce different results?

  8. 8

    Why does an operation on two identical lists end up in different results?

  9. 9

    Why does LINQ GroupBy produce different results when preceded by ToArray()?

  10. 10

    Why do ng-bind-html and $sanitize produce different results?

  11. 11

    Why do these two python regexes produce different results?

  12. 12

    Why does gzip on tar output always produce different results?

  13. 13

    Why does LINQ GroupBy produce different results when preceded by ToArray()?

  14. 14

    SQL In and having-clause, why do they produce different results?

  15. 15

    Why do ng-bind-html and $sanitize produce different results?

  16. 16

    Why does default sort produce different results with same line beginnings?

  17. 17

    Why does "wsl" produce different results when using quotes?

  18. 18

    Why does GridSearchCV on an SVC in scikit-learn produce different probabilities given identical inputs?

  19. 19

    Why does GridSearchCV on an SVC in scikit-learn produce different probabilities given identical inputs?

  20. 20

    Different results from Identical databases

  21. 21

    atof and stringstream produce different results

  22. 22

    Why is the planner coming up with different results for functions with different volatilities?

  23. 23

    Why do wkhtmltopdf.exe produce different results on different Windows machines?

  24. 24

    Why does using std::remove_reference and std::remove_const in different order produce different results?

  25. 25

    Why do wkhtmltopdf.exe produce different results on different Windows machines?

  26. 26

    Why are Python decorators with arguments syntactically different from those without?

  27. 27

    Why do these two golang integer conversion functions give different results?

  28. 28

    Why do these two python functions return different results?

  29. 29

    Why does using Array.map(parseInt) on an array of strings produce different results

HotTag

Archive