I can’t find what !! means in javascript (it’s in the Build WebServers book in module 3.1) ..what does if (!!window.EventSource) mean? (double negative? … if so, why use it?)
Hi.
It’s to make sure that the result of window.EventSource is converted to a boolean variable, even if it doesn’t return a boolean type.
Regards,
Sara
humm. ok. I’ll take your word on that, and obviously it works. Strange that I can’t find any language reference that explains that operator, and I’ve never seen anything like it before. Would you have a link to something that defines it, or parse it out a bit? Thanks.
Hi.
I was also confused about that. It’s the notation used by the library examples. So, we decided to keep it like that.
https://riptutorial.com/javascript/example/3047/double-negation—-x-
Regards,
Sara
Hi Sara, thank you for the link which nicely explains it. It also helped me understand negation of numerical variables versus string variables:
x !== 0 // instead of !!x in case x is a number
x != null // instead of !!x in case x is an object, string or undefined
so assuming window.EventSource is a number (it’s return value), another way to write the
statement would be: if (window.EventSource !== 0). But if it’s not a number,
it would be if (window.EventSource !=0). !! isn’t so bad after all 🙂 Thanks, and this can be closed out.