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
Nina Kozlova
11,602 PointsI am puzzled by the code: $("#submit").prop("disabled", !canSubmit()); what does an exclamation mark mean
The canSubmit function is declared earlier in the code. And then later we're setting the property too an element with the id "submit" as disabled. And then this !functhion() is added. What does that do?
Thank you.
1 Answer
Matt F.
9,518 PointsHi Nina,
$("#submit").prop("disabled", !canSubmit());
This code finds the element with the id of submit and assigns a property 'disabled' to it, with a value of true or false, depending on what is returned by canSubmit.
If canSubmit() returns a truthy value ("string", true, 1, 2, 3, etc.), the exclamation mark - which is the NOT operator in JavaScript (also referred to as a 'bang') - will explicitly coerce that return value to to the boolean false. This would mean that the element with an id of submit would have a prop disabled='false
Similarly, if canSubmit returns a falsy value (false, 0, null, undefined) it will explicitly coerce the value to the boolean true. Meaning that the element with an id of submit would have a prop disabled='true'.
When you see the single bang, it means that a truthy value will become the boolean false, while falsy values will become the boolean true.
Here is a page on the NOT operator in JavaScript: https://msdn.microsoft.com/en-us/library/zz722703(v=vs.94).aspx
Nina Kozlova
11,602 PointsNina Kozlova
11,602 PointsThank you, Matt!