File tree Expand file tree Collapse file tree 2 files changed +62
-0
lines changed
demos/undefined-comparison Expand file tree Collapse file tree 2 files changed +62
-0
lines changed Original file line number Diff line number Diff line change 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 )
Original file line number Diff line number Diff line change 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 >
You can’t perform that action at this time.
0 commit comments