This question has been answered quite thoroughly, but I'd like to add an answer that I hope is as simplified as possible, making the meaning of !! as simple to grasp as can be.īecause JavaScript has what are called "truthy" and "falsy" values, there are expressions that when evaluated in other expressions will result in a true or false condition, even though the value or expression being examined is not actually true or false.įor instance: if (document. Keep in mind that these are JSX and TypeScript conventions, not ones inherent to JavaScript.īut if you see strange 0s in your rendered JSX, think loose falsy management. The exception for TypeScript? If myObject was an any, you're back in JavaScript's Wild West and can return it without !!, even if your return type is a Boolean. You have to return !!myObject (or cast to the proper Boolean another way) to match TypeScript's expectations. This means, iff myObject is strongly typed, return !myObject works for a function returning a Boolean, but return myObject doesn't. The same deal in TypeScript: If you have a function that returns a Boolean (or you're assigning a value to a Boolean variable), you can't return/assign a boolean-y value it has to be a strongly typed boolean. Here are the three approaches: // this won't tell us anything about HTML5 `` as a feature catch to handle any possible errors (because they're gross) and also we don't want to use a check inside the function that won't consistently reveal the truth about the feature (for example, document.createElement('audio') will still create an element called even if HTML5 is not supported). We want to write a function that returns true if a user's browser supports the HTML5 element, but we don't want the function to throw an error if is undefined and we don't want to use try. There is a use-case where we might want to know the truth about a value, even if we expect the value to be false (or falsey), or if we expect the value not to be typeof boolean.Ĭonsider a concise function which detects feature functionality (and in this case, platform compatibility) by way of dynamic typing (aka "duck typing"). What we wish to determine in the comparison is the "truth" about the value of a reference, not the value of the reference itself. The truth is that false is not not false (that's why !!false results in false) The truth is that true is not not true (that's why !!true results in true) !! determines the "truth" of what a value is not not: The truth is that true is not false (that's why !true results The truth is that false is not true (that's why !false results ! determines the "truth" of what a value is not: ![]() It is the double-use of ! - which is the logical "not" operator. If( ( key ) != -1 )else if( Object.keys().!! is not an operator. ![]() I was pouring through the source of the THREEx extension library built on top of Three.JS and noticed this function = function(keyDesc) I am getting back into web development, and have been trying to go over the nuances of jscript recently. javascript not equal Phoenix Logan not equal not equal value OR type View another examples Add Own solution Log in, to leave a comment 4 6 Awgiedawgie 104555 points < less than or equal to x < 8 true Thank you 6 4 (4 Votes) 0 0 8 Awgiedawgie 104555 points x > y Thank you 8 0 0 4.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |