Error with return statement in javascript

0 votes

I have this javascript code which is not working.

function myfun()
   {
       return
       {
       alert("para"); 
       } 
   };

   myfun();

I have read about the javascript's automatic semicolon insertion. I corrected the above code as

return{
       alert("para"); 
       } 

But still got the error : unexpected token (. I wonder why?

NOTE: I don't need the solution but I need the explanation why the above code in not working.

EDIT

According to the book, javascript the good parts, the return statement if returns value must be in same line as return expression.

ie

 return {
status: true
};

AND

 return
{
status: true
};

Is wrong.Then How come

function myfun(para)
   {
     var status; 

       return
       {
          status : alert(para)
       };
   };


  myfun("ok");

produce no error.It won't work but shows no error as well.It works when the { is in the same line as return.

ie

 function myfun(para)
   {
     var status; 

       return{
          status : alert(para)
       };
   };

   myfun("ok");
Sep 29, 2023 in Web Development by Avinash
• 520 points

edited 4 days ago 14 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP