Skip to content

Latest commit

 

History

History
43 lines (35 loc) · 1010 Bytes

File metadata and controls

43 lines (35 loc) · 1010 Bytes

Truthiness

Truthiness is the process of simplifying any value to true or false, like during if statements or conditionals.

const something = {}
if (something) {
  console.log(`something is truthy`)
}

// You can also use the NOT operator to get falsyness
!null // true
!'a'  // false

First, understand which values are falsy.

Boolean(false)     	// yep
Boolean(0)					// zero
Boolean(-0)					// this exists
Boolean('')					// the empty string
Boolean(null)				// null
Boolean(undefined)	// undefined variables
Boolean(NaN)				// always false

Anything that is not falsy is truthy.

Boolean(true)	// yep
Boolean(-4)		// non-zero numbers
Boolean(' ')	// non-empty strings
Boolean({})		// any object, array, function, etc.
Boolean([])		// even if the object/array is empty

A developer can easily test the existence of text input or option values on an object.

if (formField.value) {
  console.log('value is probably a string with at least one character');
}