Vacuous Truth in JS
Just the other day I was testing a permissions function when I came across something very interesting about Array.prototype.every. So, let me give you a little back story. The function I was testing is a a very simple one that check whether a user has permissions required to access given module or action. This function happened to use the JS array method called every How every works is that it loops through the array and checks whether all items satisfy a provided condition. If it encounters any item that doesn’t it break the loop and returns false, however if all items satisfy the conditions then true is returned. However, there is a peculiar behavior to this method in that if an empty array is used then true is still returned. This is where the concept of vacuous truth comes in // typescript const arr1 = [2, 4, 7, 8, 10] const arr2 = [2, 4, 6, 8, 10] const arr3 = [] const checkIfAllAreEven = (items: number[]) => { return items.every((item)=> item % 2 === 0) } let allAreEven = checkIfAllAreEven(arr1) console.log(allAreEven) // logs false allAreEven = checkIfAllAreEven(arr2) console.log(allAreEven) // logs true allAreEven = checkIfAllAreEven(arr3) console.log(allAreEven) // logs true The concept of vacuous truth took me back my campus days when I used to study mathematics and getting fascinated with discrete mathematics. I never knew this rabbit hole that I got myself into would re-ignite my interest for discrete mathematics. So what are vacuous truths? Vacuous truths are conditional statements with false antecedent. When the antecedent is false, we aren’t able to infer any truth value about the consequent. A statement can also said to be vacuous is the subject of the statement doesn’t exist. So when using every method, the logical statement is all items satisfy a given condition. In the code example above, we want to satisfy that all numbers are even, but there are no numbers for arr3. References MDN Web Docs Array.prototype.every() - JavaScript | MDN Dr. Trefor Bazett Vacuously True Statements Carneades.org What is Vacuous Truth? William Spaniel Logic 101 (#13): Why Are "Vacuously True" Statements True? Dr. Trefor Bazett The Empty Set & Vacuous Truth

Just the other day I was testing a permissions function when I came across something very interesting about Array.prototype.every. So, let me give you a little back story. The function I was testing is a a very simple one that check whether a user has permissions required to access given module or action. This function happened to use the JS array method called every
How every works is that it loops through the array and checks whether all items satisfy a provided condition. If it encounters any item that doesn’t it break the loop and returns false, however if all items satisfy the conditions then true is returned. However, there is a peculiar behavior to this method in that if an empty array is used then true is still returned. This is where the concept of vacuous truth comes in
// typescript
const arr1 = [2, 4, 7, 8, 10]
const arr2 = [2, 4, 6, 8, 10]
const arr3 = []
const checkIfAllAreEven = (items: number[]) => {
return items.every((item)=> item % 2 === 0)
}
let allAreEven = checkIfAllAreEven(arr1)
console.log(allAreEven) // logs false
allAreEven = checkIfAllAreEven(arr2)
console.log(allAreEven) // logs true
allAreEven = checkIfAllAreEven(arr3)
console.log(allAreEven) // logs true
The concept of vacuous truth took me back my campus days when I used to study mathematics and getting fascinated with discrete mathematics. I never knew this rabbit hole that I got myself into would re-ignite my interest for discrete mathematics.
So what are vacuous truths? Vacuous truths are conditional statements with false antecedent. When the antecedent is false, we aren’t able to infer any truth value about the consequent. A statement can also said to be vacuous is the subject of the statement doesn’t exist. So when using every method, the logical statement is all items satisfy a given condition. In the code example above, we want to satisfy that all numbers are even, but there are no numbers for arr3.