javascriptautomatic-semicolon-insertion

I've heard Javascript inserts ";" automatically and that may cause problems


I've also heard that Go insert them too, but they followed a different approach

How does Javascript insert semicolons while interpreting?


Solution

  • One of the biggest things I've found is, let's say you have a function that returns coordinates (or any object really), similar to this.

    function getCoordinates() {
        return
            {
                x: 10,
                y: 10
            };
    }
    

    You would expect to get back an object right? WRONG! You get back undefined. The interpreter converts the code into this

    function getCoordinates() {
        return;
            {
                x: 10,
                y: 10
            };
    }
    

    Since return by itself is a valid statement. You need to be sure to write the return as follows

    function getCoordinates() {
        return {
                x: 10,
                y: 10
            };
    }