Skip to content

Instantly share code, notes, and snippets.

@TGITS
Created November 17, 2024 16:56
Show Gist options
  • Save TGITS/e19f6d763b7f90331f2d0a52d29a24bc to your computer and use it in GitHub Desktop.
Save TGITS/e19f6d763b7f90331f2d0a52d29a24bc to your computer and use it in GitHub Desktop.
Comparing boolean or operator and null coalescing operator
const i_am_null = null;
const i_am_undefined = undefined;
const i_am_non_null = 'I have value !'
const i_am_false = false;
const zero = 0;
const empty_string = '';
console.log(`Comparing the evaluation of (i_am_null ?? 'default value') and (i_am_null || 'default value'):
${i_am_null ?? 'default value'} <-> ${i_am_null || 'default value'}`);
console.log(`Comparing the evaluation of (i_am_undefined ?? 'default value') and (i_am_undefined || 'default value'):
${i_am_undefined ?? 'default value'} <-> ${i_am_undefined || 'default value'}`);
console.log(`Comparing the evaluation of (i_am_non_null ?? 'another value') and (i_am_non_null || 'another value'):
${i_am_non_null ?? 'another value'} <-> ${i_am_non_null || 'another value'}`);
console.log(`Comparing the evaluation of (i_am_false ?? 'default value') and (i_am_false || 'default value'):
${i_am_false ?? 'default value'} <-> ${i_am_false || 'default value'}`);
console.log(`Comparing the evaluation of (zero ?? 42) and (zero || 42):
${zero ?? 42} <-> ${zero || 42}`);
console.log(`Comparing the evaluation of (empty_string ?? "Non-empty") and (empty_string || "Non-empty"):
${empty_string ?? "Non-empty"} <-> ${empty_string || "Non-empty"}`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment