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 JavaScript and the DOM (Retiring) Getting a Handle on the DOM Select a Page Element By Its ID

guram dgebuadze
seal-mask
.a{fill-rule:evenodd;}techdegree
guram dgebuadze
Front End Web Development Techdegree Student 4,122 Points

One line arrow function why can't I write this arrow function on one line? without curly braces

const myHeading = document.getElementById('myHeading');

myHeading.addEventListener( 'click', () => { myHeading.style.color = 'red'; });

2 Answers

It is possible. You would need to remove the curly braces as mention as well as the first semi-colon after "red". Currently when the JS engine reads this statement it ends before that closing parenthesis for the event listener. If you look in your console.log you will see the error "missing ) after argument list" since the statement ended before the closing parenthesis.

myHeading.addEventListener( 'click', () => myHeading.style.color = 'red' );