JavaScript/JXA WARNING! Watch Your "return" Statements Closely!

JavaScript/JXA doesn't care about whitespace in lines, right? For example, you can place a "{" at the end of a line, or at the start of a new line, right?

Try running this:

function test1() {
    return "test1";
}

function test2() {
    return
        "test2";
}

console.log("Result of test1: '" + test1() + "'");
console.log("Result of test2: '" + test2() + "'");

If you're running this from Script Editor, you'll need to select the "Messages" tab at the bottom.

1 Like

Perhaps the “return” statement is different.
This works:

function test2() {
    return "a" +
    "test2";
}

test2();

Have you tried this in a pure JavaScript script in a browser?

The thing to learn is the rules of automatic semi-colon insertion:

http://www.ecma-international.org/ecma-262/5.1/#sec-7.9.1

2 Likes

The issue is if a return statement is on a line by itself, the compiler automatically inserts a semicolon after it. As Rob’s link shows.