Why does this not cause a [JavaScript] error?

If you run this code in the console:

(function(){
    test: "hello";
})();

Or even this:

test: "hello";

Nothing happens and no error occurs. What for? It's as simple as, "Good ... This is JavaScript ... Deal with it."

In the context, some interns at my company wrote something similar by chance (which means using "="). When they were faced with the fact that this is likely to cause an error, they said that this did not happen.

And now I'm curious.

+4
source share
1 answer

( , , ) JavaScript (. ECMAScript 2015 MDN). , , break continue.

(function(){     // Begin function expression
    test:        // Label statement
       "hello";  // Create a string and do nothing with it
})();            // End function expression and invoke the created function
+12

All Articles