Skip to content

Quirks of JavaScript boolean logic

Notifications You must be signed in to change notification settings

themithy/almost-false

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 

Repository files navigation

function almostAlwaysTrue(a) {
  console.log(a == a)
}

almostAlwaysTrue(NaN) // false - this is easy

// -----

function usuallyFalse(a) {
  console.log(!a == a)
}

usuallyFalse('0')                 // true
usuallyFalse(' ')                 // true
usuallyFalse([])                  // true
usuallyFalse([undefined])         // true
usuallyFalse([null])              // true
usuallyFalse([false])             // false - outlier
usuallyFalse([0])                 // true
usuallyFalse([''])                // true
usuallyFalse([' '])               // true
usuallyFalse(['0'])               // true
usuallyFalse(new Boolean(false))  // true - in fact, this is the most bizarre
usuallyFalse(new String())        // true

// -----

function inMostCasesTrue(a) {
  console.log(a >= a)
}

inMostCasesTrue(NaN)        // false
inMostCasesTrue(undefined)  // false

// -----

function couldThisBeTrue(a) {
  console.log(a == a + 1)
}

couldThisBeTrue(2**53)  // true - float64 precision loss

// -----

About

Quirks of JavaScript boolean logic

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published