Skip to content

Instantly share code, notes, and snippets.

@scytacki
Created January 27, 2026 15:54
Show Gist options
  • Select an option

  • Save scytacki/7b932e7cd22f15f6cab0fd5a6d42f891 to your computer and use it in GitHub Desktop.

Select an option

Save scytacki/7b932e7cd22f15f6cab0fd5a6d42f891 to your computer and use it in GitHub Desktop.

It is tempting to use default positive values for object properties. This can make the code more readable when in most cases the value should be positive. However it is easy to accidentally pass undefined for the value. That then makes the value the function sees true. So now the function behaves the opposite way you probably intended when you called it.

More details here:

function doSomething({enabled = true}) { 
  console.log("enabled", enabled);
}
const check = true;
const valueThatMightBeDefined = undefined;
doSomething({enabled: check && valueThatMightBeDefined});

The idea is to have a function that is enabled by default, but it can be disabled by passing a false value. You might think that calling it like above would cause it to print enabled false. But because check && valueThatMightBeDefined evaluates to undefined, that means that we are passing {enabled: undefined} so then the default value is used (true).So the moral of the story is that using boolean true defaults like enabled = true is not good since it is common practice to pass falsey values which could easily be undefined.

Note: you can’t use typescript to force enabled to be a boolean (so undefined is not allowed), because then you can’t have a default.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment