Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript DOM Scripting By Example Improving the Application Code Refactor 2: Readable Branching Logic

James Estrada
seal-mask
.a{fill-rule:evenodd;}techdegree
James Estrada
Full Stack JavaScript Techdegree Student 25,866 Points

Question about arrow functions as values in object literal

As I understand, an arrow function is a function expression, therefore a semicolon is more technically correct to have at the end. This has been the standard practice as a standalone arrow function, but when it's inside an object literal as a value, the console gives me a syntax error when I add the semicolon.

const nameActions = {
      remove: () => {
        ul.removeChild(li);
      };, // why applying standard practice throws an error?
      ...
      ...
};

The above code throws an Uncaught SyntaxError: Unexpected token ';' Why?

1 Answer

Steven Parker
Steven Parker
231,271 Points

Semicolons do not go at the end of expressions, they go at the end of statements.

When you create "a standalone arrow function", the arrow function expression would be the last thing in an assignment statement. In that situation, a semicolon would be appropriate (and a "best practice").

In the object literal, the expression is not part of a statement, so a semicolon is incorrect syntax.

Steven Parker
Steven Parker
231,271 Points

Essentially, expressions are evaluated to produce a value, and statements are executed to make something happen.

This is a statement that declares the variable "a" and gives it a value. It's making something happen.

On the other hand, the right side of an assignment (in this case, just "2") would be an expression. It produces a value.