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 trialSara Masek
Front End Web Development Techdegree Student 11,513 PointsWhy does !search equate to falsey but null/undefined don't?
Hi all, I was wondering if someone could maybe put the concept of falsey into simpler terms for me. In the video Guil was able to set the first conditional statement to falsey by typing "!search", which makes sense because I understand that putting "!" in front of a variable means "not". What I don't understand is why when I try and change the conditional to something like "search === undefined" or "search === null" all that pops up is the html, but no values have been plugged in. This tells me the program doesn't recognize these conditionals as falsey (unless I'm mistaken) even though I know they're falsey values.
Thanks for any feedback! This question comes from the "Search for a Value in an Array" video.
const inStock = ['pizza', 'cookies', 'eggs', 'apples', 'milk', 'cheese', 'bread', 'lettuce', 'carrots', 'broccoli', 'potatoes', 'crackers', 'onions', 'tofu', 'limes', 'cucumbers'];
const search = prompt('Search for a product.');
let message;
if(!search){
message = `<strong>In stock:</strong> ${inStock.join(', ')}`;
}else if (inStock.includes(search.toLowerCase())) {
message = `Yes we have <strong>${search}!</strong>It's #${inStrock.indexOf(search.toLowerCase())+ 1} on the list!`;
}else{
message = `Sorry we do not have <strong>${search}!</strong>`;
}
document.querySelector("main").innerHTML = `<p>${message}</p>`;
2 Answers
Terfa Nomhwange
6,568 PointsHi Sara. Can you share your code?
Terfa Nomhwange
6,568 PointsIts tricky to explain. But i don't think JavaScript will allow you to use the comparison operator(===) which means 'equal value and equal type' on an if statement when there is no value for the if statement to hold on the left and compare to what is on the right i.e referring to your intended code. Falsy values are values that JavaScript's built-in type coercion converts to false or in a Boolean context are considered false or values that do not exist and there are several of them (null, undefined, NaN, false, ' ',0). Bear in mind that you can't say undefined===false, but it appears this is what you are trying to do.
Sara Masek
Front End Web Development Techdegree Student 11,513 PointsI was trying to set search (not !search) to other falsey values, such as "undefined" or "null" using the "===" operator.
Sara Masek
Front End Web Development Techdegree Student 11,513 PointsSara Masek
Front End Web Development Techdegree Student 11,513 Pointsupdated the question with the code, sorry about that!