+ 1

Help please 😓😓😓

I’m reading a book about js, and in a library I found this peace of code: CanvasRenderingContext2D.prototype.cartesian = function(on) { this.iscartesian = !!on; return this; } So, what is the meaning of “!!”?

26th Feb 2018, 5:03 PM
Chob
Chob - avatar
5 Answers
+ 5
It is a trick to force the value of on to be boolean. Zero is false and non-zero is true. If on has 5, !5 becomes false and !!5 becomes true so iscartesian will only have true or false values.
26th Feb 2018, 5:25 PM
John Wells
John Wells - avatar
+ 3
@maxcookmax I click on the star before I reply so I get notification of other people responding. I also refresh the thread afterwards because notifications can be delayed. However, having multiple right answers are not a problem. Better that over none.
26th Feb 2018, 6:46 PM
John Wells
John Wells - avatar
+ 2
@John thank you very much, very helpful. @maxcookmax Thank you too, I will try. I have one other question, Does this trick works in other programming languages??
26th Feb 2018, 5:45 PM
Chob
Chob - avatar
+ 2
In some, yes. Others, do not support implicit typecasts and generate errors.
26th Feb 2018, 5:51 PM
John Wells
John Wells - avatar
+ 2
Thank you again John
26th Feb 2018, 5:55 PM
Chob
Chob - avatar