Last active
January 28, 2020 15:49
-
-
Save Weiyuan-Lane/59332e04a1f2a5ef401f9fa4eb772709 to your computer and use it in GitHub Desktop.
Showcase nullish coalescing in JavaScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const strVar = ''; | |
const numVar = 0; | |
const boolVar = false; | |
const nullVar = null; | |
const undefinedVar = undefined; | |
// Example for OR operator || | |
console.log("strVar || 'default string' = "); | |
console.log(`'${strVar || 'default string'}'`); // Output will be 'default string' | |
console.log("numVar || 100 = "); | |
console.log(numVar || 100); // Output will be 100 | |
console.log("boolVar || true = "); | |
console.log(boolVar || true); // Output will be true | |
console.log("nullVar || 'another string' = "); | |
console.log(`'${nullVar || 'another string'}'`); // Output will be 'another string' | |
console.log("undefinedVar || 'undefined string' = "); | |
console.log(`'${undefinedVar || 'undefined string'}'`); // Output will be 'undefined string' | |
// Example for nullish operator ?? | |
console.log("strVar ?? 'default string' = "); | |
console.log(`'${strVar ?? 'default string'}'`); // Output will be '' | |
console.log("numVar ?? 100 = "); | |
console.log(numVar ?? 100); // Output will be 0 | |
console.log("boolVar ?? true = "); | |
console.log(boolVar ?? true); // Output will be false | |
console.log("nullVar ?? 'another string' = "); | |
console.log(`'${nullVar ?? 'another string'}'`); // Output will be 'another string' | |
console.log("undefinedVar ?? 'undefined string' = "); | |
console.log(`'${undefinedVar ?? 'undefined string'}'`); // Output will be 'undefined string' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment