+ 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 “!!”?
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.
+ 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.
+ 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??
+ 2
In some, yes. Others, do not support implicit typecasts and generate errors.
+ 2
Thank you again John