1

What is the difference in writing some object as !!! or ! in javascript.

var d is an object white may have some property.

if(!!!d.c.l) or if(!d.c.l) what is the difference

Mayank Sharma
  • 89
  • 1
  • 8
  • I have not seen !!! , it must be !! , !! Operator converts anything into a boolean value – Nishant Jun 23 '20 at 09:38
  • 3
    Does this answer your question? [The use of the triple exclamation mark](https://stackoverflow.com/questions/21154510/the-use-of-the-triple-exclamation-mark) – Álvaro Tihanyi Jun 23 '20 at 09:39

1 Answers1

2

There's no difference (in the result) between ! and !!!

the ! parses the value to a boolean of opposite type. The examples below make it more clear.

EG

const val = true;
!val // this is false

by doing ! multiple times you just swap the value again.

EG

const val = true;
!val // this is false
!!val // this is true
!!!val // this is false
Joe Lloyd
  • 19,471
  • 7
  • 53
  • 81
  • a good exmaple is if you have a function and return for example this `return someVariable ? true : false` so you can write it like this `return !!someVariable` – bill.gates Jun 23 '20 at 09:42
  • My mentor suggested I use this as I was using! if(!!nextProps.selectedView && !!nextProps.selectedView.selectedQuestion !== !!this.props.selectedView.selectedQuestion) { route(`/practice-question?clid=${nextProps.classCode}&cpid=${nextProps.chapterId}&tid=${nextProps.selectedView.selectedTopic.id}#`,true) } what if i use single ! – Mayank Sharma Jun 23 '20 at 10:06
  • a single will make true false and false true. a double will make false false and true true. They are just opposite. most of the time you don't need `!!` its actually a shorthand for parsing one type to boolean. – Joe Lloyd Jun 23 '20 at 10:11