Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
226 changes: 226 additions & 0 deletions truthy-falsy-demo/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
<!DOCTYPE html>
<html>
<head>
<!-- DONE TODO: Change the title of the page to your demo name -->
<title>Truthy & Falsy Values Demo | Bit by Bit</title>
<link rel="stylesheet" href="style.css">
<link rel="icon" type="image/png" href="https://bitbybitcoding.org/imgs/favicon.svg">
<link href="https://fonts.googleapis.com/css2?family=Anonymous+Pro&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Nunito:wght@200&display=swap" rel="stylesheet">
<script src="https://kit.fontawesome.com/7f9874946f.js" crossorigin="anonymous"></script>
</head>
<body>
<div id="header" class="flex-container-header">
<div class="flex-item-header flex-container-left">
<div class="flex-item-left">
<a href="/" style="margin-right: 1em;">
<button id="back-button" title="Back to all demos">
<i class="fas fa-arrow-left"></i>
</button>
</a>
</div>
<!-- DONE TODO: Change the name of the demo here -->
<h1 class="flex-item-left">TRUTHY & FALSY VALUES DEMO</h1>
</div>
<div class="flex-item-header">
<img id="bxb-logo" src="https://bitbybitcoding.org/imgs/Logo.svg" alt="Logo">
</div>
</div>
<div class="content">
<!-- DONE TODO: Fill this content div with all the content of the demo -->
<div class="section">
<h2>Introduction</h2>
<p>
In JavaScript, boolean (true or false) types and comparisons are not the only forms of truthy and falsy
values. But first, what are truthy and falsy values? <br> <br>
As the names suggest...
<ul>
<li>Truthy expressions result in the boolean value true</li>
<li>Falsy expressions result in the boolean value false</li>
</ul>

<img id="bit-by-bot-intro" src="../assets/bit-by-bot-images/waving-happy-bot.svg" alt="Bit by Bot">

Here are the falsy values:
</p>
<table>
<tr>
<th>Falsy Value</th>
<th>Description</th>
</tr>
<tr>
<td>
<div class="code codePartTwo">false</div>
</td>
<td>If the boolean variable is set to be false, then it will evaluate to false.</td>
</tr>
<tr>
<td>
<div class="code codePartTwo">0</div>
</td>
<td>If the value of the number is 0, then it will be considered as falsy.</td>
</tr>
<tr>
<td>
<div class="code codePartTwo">"" or ''</div>
</td>
<td>If the string is blank (empty), then it will evaluate to false.</td>
</tr>
<tr>
<td>
<div class="code codePartTwo">null</div>
</td>
<td>If there is no value at all, then JavaScript will return a value called null, which will also be considered as falsy.</td>
</tr>
<tr>
<td>
<div class="code codePartTwo">undefined</div>
</td>
<td>If a variable is declared but not assigned a value, then it will return a value called undefined and will also evaluate to false.</td>
</tr>
<tr>
<td>
<div class="code codePartTwo">NaN</div>
</td>
<td>If the value is not a number, (e.g. divide a string by a number), then NaN (Not a Number) will be returned and will evaluate to false.</td>
</tr>
</table>
<p class="center note">** NOTE: Values other than the ones listed above are truthy values! **</p>
</div>
<div class="section">
<h2>Test it Out!</h2>
<p>Go through each example below to see whether or not they will evaluate to false!</p>
<img id="bit-by-bot-interactive" src="../assets/bit-by-bot-images/computer-open-bot.svg" alt="Bit by Bot Using a Computer">
</div>
<!-- row 1 of the examples-->
<div class="flex-box">
<div class="flex-col section">
<p class="type center">false:</p>
<div class="code">
<div class="codePartOne">let friday</div>
=
<div class="codePartTwo">false</div>;
<br>
<div class="codePartOne">if</div>
<div class="codePartThree"> (!friday) {</div>
<br>
<div class="codeInside">alert("Today is not Friday!");</div>
<br>
<div class="codePartThree">}</div>
<br>
<br>
</div>
<div class="center">
<button onclick="falseValue()">Run</button>
</div>
</div>
<div class="flex-col section">
<p class="type center">0:</p>
<div class="code">
<div class="codePartOne">let grade</div>
=
<div class="codePartTwo">0</div>;
<br>
<div class="codePartOne">if</div>
<div class="codePartThree"> (!grade) {</div>
<br>
<div class="codeInside">alert("This is not a valid grade!");</div>
<br>
<div class="codePartThree">}</div>
<br>
<br>
</div>
<div class="center">
<button onclick="zero()">Run</button>
</div>
</div>
</div>
<!-- row 2 of the examples-->
<div class="flex-box">
<div class="flex-col section">
<p class="type center">Empty String:</p>
<div class="code">
<div class="codePartOne">let sentence</div>
=
<div class="codePartTwo">""</div>;
<br>
<div class="codePartOne">if</div>
<div class="codePartThree"> (!sentence) {</div>
<br>
<div class="codeInside">alert("This is a blank string!");</div>
<br>
<div class="codePartThree">}</div>
<br>
<br>
</div>
<div class="center">
<button onclick="emptyString()">Run</button>
</div>
</div>
<div class="flex-col section">
<p class="type center">null Value:</p>
<div class="code">
<div class="codePartOne">let rank</div>
=
<div class="codePartTwo">null</div>;
<br>
<div class="codePartOne">if</div>
<div class="codePartThree"> (!rank) {</div>
<br>
<div class="codeInside">alert("Your rank is currently unavailable!");</div>
<br>
<div class="codePartThree">}</div>
<br>
<br>
</div>
<div class="center">
<button onclick="nullValue()">Run</button>
</div>
</div>
</div>
<!-- row 3 of the examples-->
<div class="flex-box">
<div class="flex-col section">
<p class="type center">undefined Value (unassigned value):</p>
<div class="code">
<div class="codePartOne">let word</div>;
<br>
<div class="codePartOne">if</div>
<div class="codePartThree"> (!word) {</div>
<br>
<div class="codeInside">alert("This variable has not been assigned a value yet!");</div>
<br>
<div class="codePartThree">}</div>
<br>
<br>
</div>
<div class="center">
<button onclick="undefinedValue()">Run</button>
</div>
</div>
<div class="flex-col section">
<p class="type center">NaN Value (invalid number):</p>
<div class="code">
<div class="codePartOne">let food</div>
=
<div class="codePartTwo">"jelly" / 3</div>;
<br>
<div class="codePartOne">if</div>
<div class="codePartThree"> (!food) {</div>
<br>
<div class="codeInside">alert("There is a problem with the number!");</div>
<br>
<div class="codePartThree">}</div>
<br>
<br>
</div>
<div class="center">
<button onclick="notANumber()">Run</button>
</div>
</div>
</div>
<!-- The end of the content div is here -->
</div>
</body>
<script src="script.js"></script>
</html>
41 changes: 41 additions & 0 deletions truthy-falsy-demo/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
function falseValue() {
let friday = false;
if (!friday) {
alert("Today is not Friday!");
}
}

function zero() {
let grade = 0;
if (!grade) {
alert("This is not a valid grade!");
}
}

function emptyString() {
let sentence = "";
if (!sentence) {
alert("This is a blank string!");
}
}

function nullValue() {
let rank = null;
if (!rank) {
alert("Your rank is currently unavailable!");
}
}

function undefinedValue() {
let word;
if (!word) {
alert("This variable has not been assigned a value yet!");
}
}

function notANumber() {
let food = "jelly" / 3;
if (!food) {
alert("There is a problem with the number!");
}
}
Loading