-
Notifications
You must be signed in to change notification settings - Fork 0
Home
This repository is part of a larger project!
Like many other languages, javascript has boolean data types which are used to falsify statements. Most Javascript values are inherent true (truthy).
The following operators are examples which work with boolean values, but actually accept any values:
Operator | Name |
---|---|
if | if statement |
|| | Logical OR |
&& | Logical AND |
Beside the inherent truthy values, there exist exactly seven falsy ones. These are:
Value | Description |
---|---|
false | boolean value |
0 | number value |
-0 | number value |
"" | Empty string value |
NaN | Not a Number |
null | Absence of any object value |
undefined | Variable has not been assigned a value |
The values empty string and 0 could be troublesome. As an example a sample of the function unwanted in "truthiness.js" shall be taken:
function unwanted(first, second)
{
if(!first)
{
first = 200;
}
if(!second)
{
second = 300;
}
}
Giving the parameters "first" and "second" the value of empty string and/or 0 will activate the definition of the if statements.
A solution for undefined values should be found in the function wanted in "truthiness.js"
The user interaction part after setting some input values should look like the content as seen below by starting "index.html" in a web browser.
Depending what the values are in "Setting Values" the results are shown in "Unwanted Results" and "Wanted Results" by pressing the button "Calculate".
-
"Unwanted Results" shows the results of the function unwanted in the file "truthiness.js".
-
"Wanted Results" shows the results of the function wanted in the file "truthiness.js".
Note that all files should be placed in the same folder so that the functionality of the code is guaranteed.
This knowledge was gained:
Effective JavaScript "68 Specific Ways to Harness the Power of JavaScript" by David Herman