Javascript - an explanation of behavior request

I witnessed this behavior from JavaScript, which I found interesting, but I don’t know how to look for the cause of this, so I have the following question.

As far as I know, multi-line operators work in JavaScript, for example:

var text = "abc" + 
"xyz";

But when it is:

// returns undefined
return 
"some text";

Noo! He returns undefined.
This is a basic statement that should return a string. But this version does not work as I expect.

So what are we experiencing here? I'm just curious.

+4
source share
2 answers

The problem is the horror that automatically inserts a semicolon . JavaScript rules for ASI are inserted ;after return, giving you the following:

return;
"some text";

, — return , undefined, , ( , ).

, , ( ), , , ASI , , parens:

return (
    "some text"
);

( , , .)

, , ASI, . , return, , . :

return
{
     result: "success"
};

, ASI, .

: - , , return. , ASI .

Axel Rauschmayer ASI / ( ). :

a
++
c

... ASI

a;
++
c

... .: -)

+9

return / . . return undefined .

+1

Source: https://habr.com/ru/post/1569543/


All Articles