Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save gilles-gardet/919f94ecad87fc85d59306c438e3a1b9 to your computer and use it in GitHub Desktop.
Save gilles-gardet/919f94ecad87fc85d59306c438e3a1b9 to your computer and use it in GitHub Desktop.
let data = undefined ?? 'noData'
console.log(data) // noData
data = null ?? 'noData'
console.log(data) // noData
data = '' ?? 'noData'
console.log(data) // ''
data = 0 ?? null ?? 'noData'
console.log(data) // 0
// when we assign value based on itself value it is "Shorthand" way
data ??= 'noData'
console.log(data) // 0
Confuse 😕 with OR(||) Operator ??
OR operator used when you want conditionally assign other value if the value is not truthy(0,’ ’, null, undefined, false, NaN)
let data = undefined || 'noData'
console.log(data) // noData
data = null || 'noData'
console.log(data) // noData
data = '' || 'noData'
console.log(data) // noData
data = 0 || null || 'apple'
console.log(data) // apple
// when we assign value based on itself value it is "Shorthand" way
data ||= 'noData'
console.log(data) // apple
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment