JavaScript JavaScript and the DOM Getting a Handle on the DOM Select a Page Element By Its ID

guram dgebuadze
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

Elijah Quesada
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Elijah Quesada
Front End Web Development Techdegree Graduate 32,961 Points

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' );