1
// options
if (options) {
  this.deep = !!options.deep
  this.user = !!options.user
  this.lazy = !!options.lazy
  this.sync = !!options.sync
} else {
  this.deep = this.user = this.lazy = this.sync = false
}

While i was trying to understand vue.js Watcher i saw this syntax !!options.deep here i totally understand what ! means, but why would anyone want to use !!true because it will give you true again.

Thanks

rummykhan
  • 2,603
  • 3
  • 18
  • 35

2 Answers2

5

If the variable is a boolean, then !! won't have any effect but if the variable is a number or another type, then it will be converted to a boolean.

!!false === false
!!true  === true
!!0     === false
!!1     === true
!!x     === Boolean(x)
3

!! is an attempt to make coercion to Boolean in JavaScript. Same result will be with Boolean(value)

uladzimir
  • 5,639
  • 6
  • 31
  • 50