Skip to content

Commit b7113fb

Browse files
committed
Add comparison of undefined values test.
1 parent 6df9915 commit b7113fb

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ with.
1010

1111
## My JavaScript Demos - I Love JavaScript!
1212

13+
* [Comparing Undefined Values In JavaScript](https://bennadel.github.io/JavaScript-Demos/demos/undefined-comparison)
1314
* [Using The Button Form Attribute To Create Standalone Buttons In HTML](https://bennadel.github.io/JavaScript-Demos/demos/link-buttons)
1415
* [Box Breathing Exercise With SpeechSynthesis And Alpine.js](https://bennadel.github.io/JavaScript-Demos/demos/box-breathing-alpine)
1516
* [Using CSS Gap To Control Margins In Website Copy](https://bennadel.github.io/JavaScript-Demos/demos/margins-via-gap-css)
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1">
6+
<title>
7+
Comparing Undefined Values In JavaScript
8+
</title>
9+
</head>
10+
<body>
11+
12+
<h1>
13+
Comparing Undefined Values In JavaScript
14+
</h1>
15+
16+
<p>
17+
See console for results.
18+
</p>
19+
20+
<script type="text/javascript">
21+
22+
var comparisons = [
23+
"undefined == false",
24+
"undefined == true",
25+
"undefined == 0",
26+
"undefined > 0",
27+
"undefined >= 0",
28+
"undefined < 0",
29+
"undefined <= 0",
30+
];
31+
32+
for ( var test of comparisons ) {
33+
34+
console.group( `Testing: %c${ test }`, "color: red" );
35+
console.log( `%c${ eval( test ) }`, "background: yellow" );
36+
console.groupEnd();
37+
38+
}
39+
40+
// --------------------------------------------------------------------------- //
41+
// --------------------------------------------------------------------------- //
42+
43+
var inputs = {};
44+
45+
// The safe-navigation operator safely bypasses the indexOf() call on an undefined
46+
// property, resulting in an overall "undefined" for the expression. Then, the
47+
// comparison of "undefined" to a number is always false, skipping the if-block.
48+
if ( inputs.value?.indexOf( "hello" ) >= 0 ) {
49+
50+
console.log( "Hello Found!" );
51+
52+
} else {
53+
54+
console.log( "Hello Not Found" );
55+
56+
}
57+
58+
</script>
59+
60+
</body>
61+
</html>

0 commit comments

Comments
 (0)