Meaning of javascript error | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Meaning of javascript error

Function("(")() //Uncaught SyntaxError: Unexpected token } Function("{")() //Uncaught SyntaxError: Unexpected token ) Why does a different symbol give an error? I understand that the code is incorrect, but why is this error? <supplement> Function("+")() , Function("~")() , Function("1+")() //Uncaught SyntaxError: Unexpected token } Function("}+{")() //Uncaught SyntaxError: Single function literal required

30th Aug 2018, 1:06 AM
mar
2 Answers
+ 6
Assuming I understand your question correctly, I will attempt to explain. Function("1")() doesn't give an error because you correctly define a function prior to it executing. Putting + instead of 1 ends up with an invalid function body of {+} so it complains on seeing the } as it was expecting a valid statement in the function body. Your "}+{" ends up with {}+{} as your code yielding an error at the + after correctly finishing the function.
30th Aug 2018, 11:07 AM
John Wells
John Wells - avatar
+ 2
Thank you for answering my question.
30th Aug 2018, 12:49 PM
mar