➕︎ Aa ➖︎ |Dark 1 ● Dark 2 ● Light|Hack ● Fira Code|Back to PT Mono✖
// CPCS-324 Algorithms and Data Structures 2
// Test for nullable (missing) value demo
// 2020, Dr. Muhammad Al-Hashimi
// -----------------------------------------------------------------------
// implement required-optional item logic
// testing for equality with 'undefined' can check for: missing var
// values, missing function arguments, and missing object properties
// use equality operator === instead of the usual == (google difference)
// -----------------------------------------------------------------------
// example 1: both x,y are declared but only x has value
var x=0, y;
// x will produce false since it has a value, but y will result in true (value is missing)
document.write( "<p>Example 1: variable has no value <br>", x === undefined, ", ", y === undefined, "</p>");
// -----------------------------------------------------------------------
// example 2: object property fields
var ob = {a:3, b:4};
// there is no property "c" for the object (or perhaps missing)
document.write( "<p>Example 2: missing object fields<br>", ob.a === undefined, ", ", ob.b === undefined, ", ", ob.c === undefined, "</p>" );
// -----------------------------------------------------------------------
// we would like to develop an idiom for *acting* when an optional object
// field or function argument is specified (not missing)
// example 3: function arguments ('f' defined below with 2 args), x and y declared above
f(x); // only first argument is passed
f(x,y); // both arguments passed but second has no value (same as missing)
y = null; // null is a value!
f(x,y); // pass both parameters with values
y = ""; // the empty string is also a value!
f(x,y);
// -----------------------------------------------------------------------
// a function with 2 declared arguments (used in example 3)
// note the idiom (actionable condition expression)
function f(a,b)
{
document.write("<p>Example 3<br>1st argument of f is: ", a);
if ( ! (b === undefined) ) // read: if b specified (idiom to act on optional item)
document.write("<br>2nd argument is:<br>",b);
else
document.write("<br>2nd argument not passed or is undefined");
}