29.07.2016 Views

front-end-developer_1_

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Front-End-Developer - Level 1<br />

type $("input#age").val(); in the JavaScript console to see for yourself). An empty string<br />

isn't true or false .<br />

What's happening here is that JavaScript has concepts called truthy and falsey . Falsey<br />

values include empty strings, the number 0 , the number NaN , undefined , null (which<br />

we still haven't seen), and, of course, false itself. If JavaScript sees any of these as a<br />

branching condition, it will treat them as false. Everything else is truthy.<br />

Here's another operator you should know about: if you want to test that something is not<br />

true, you can use the ! (not) operator like this:<br />

if (!under18) {<br />

}<br />

// do something only adults can do<br />

And finally, here's a trick you'll commonly see. This code:<br />

if (something > 0) {<br />

return true;<br />

} else {<br />

return false;<br />

}<br />

can be written simply as:<br />

return something > 0;<br />

That's because the condition ( something > 0 ) evaluates to a boolean - either true or false.<br />

So we can just return that Boolean.<br />

Summary<br />

Logical operators:<br />

&& means and: g<strong>end</strong>er === 'male' && age < 26<br />

|| means or: g<strong>end</strong>er === 'male' || age < 26<br />

! means not: !under18 Empty strings, the number 0 , the number NaN , undefined ,<br />

null , and false itself are falsey. If JavaScript sees any of these as a branching<br />

condition, it will treat them as false. Everything else is truthy.<br />

More Branching<br />

128

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!