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 trialjeroenmaljers
1,708 PointsAt 5:40 in the video. Why do you need a variable? Why can't you use the function like if IsEmailIsEmpty() === true
the variable feels like an extra step
3 Answers
Steven Parker
231,236 PointsI agree with Jamie that the variable is only needed if you have a reason to capture the current state and re-use it later in the code.
I would also point out that it's completely unnecessary (and sometimes problematic) to compare anything to "true". This test can be simply written:
if (fieldTest) {
Or, without the variable:
if (isEmailEmpty()) {
Jamie Carter
Front End Web Development Techdegree Student 12,096 PointsYou could just execute the alert code within the original if statement, like your question suggests, it all depends on whether you need to save that TRUE or FALSE statement as a variable for use in other parts of your code.
jeroenmaljers
1,708 Pointsthanks for your comments
Anik Devaughn
7,751 PointsHi , I have similar question as above with " IsEmailIsEmpty() " function. I have added some HTML Form code and created the same JavaScript IsEmailIsEmpty() function as it's shown in this Video. To give you better understanding I have added my code below: index.html:
<div id="form_sample"> <h1>Learning Function()</h1> <hr /> <h2>Join our Newsletter</h2><br /> <form name="myForm" action="" method="get"> <input type="email" name="email" id="email" value=""/> <!--require --> <br /> <input type="button" name="button "value="Join NewsLetter" onclick="isEmailEmpty()"/> </form> <script src="FunctionPractice.js"></script> </div>
FunctionPractice.js:
function isEmailEmpty() { var field = document.getElementById('email'); if (field.value === "") { alert("Please provide your email address."); // when calling isEmailEmpty() onClick // eventlistener , then this lines works return true; } else { return false; } } var fieldTest = isEmailEmpty(); if (fieldTest === true) { alert("Please provide your email address."); }
The Problem i have is that when I called isEmailEmpty() onClick eventlistener , it works but if I do like this 'onclick="fieldTest" ' then nothing happens, I dont get any response when I clicked the button. Why is that? What mistake I am making that i don't get it. Isn't it the isEmailEmpty() function is saved in the ' fieldTest' and the second part of if-condition ( if (fieldTest === true) ) should works. Please can you explain clearly what i am doing wrong and how should i think. Thanks in Advance!!!