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 Functions Create Reusable Code with Functions Using Multiple return Statements

Strictly or !not operator.

https://w.trhou.se/zdh7k4g6ym

On line 3 the use of ! is confusing to grasp to i can see the logic easily of === strictly. Would this cause me problems..

1 Answer

Hi Jason

No you wont run in to issues here, you can do it either way. Remember though, that this is comparing against its falsy value

const field = document.querySelector("#info");
if (!field.value) {
   ...
}

and this is strictly checking equality.

const field = document.querySelector("#info");
if (field.value === null) {
   ...
}

Both of which have the same outcome. There is an error with your code though, if null both dont have a property value so you can completely remove that and keep it as just field, not field.value

Check out MDN docs and you will find a section called return value, this is how you know what to check for if you want to strictly check equality.

An HTMLElement object representing the first element in the document that matches the specified set of CSS selectors, or null is returned if there are no matches.

So for your case, if you find it easier to do strict equality, as stated with the MDN docs it will return either HTMLElement or null.

const field = document.querySelector("#info");
if (field === null) {
   ...
}

And if you refer the falsy way, there is no need to check null as a null value boolean is falsy

const field = document.querySelector("#info");
if (!field) {
   ...
}

Hope this helped clear things up

Have fun!