which means `~` in javascript
source link: https://www.codesd.com/item/which-means-in-javascript.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
which means `~` in javascript
I view the code of express, and see this code https://github.com/visionmedia/express/blob/master/lib/application.js#L490
if ('all' == envs || ~envs.indexOf(this.settings.env)) fn.call(this);
what the ~
means before envs
In case you were wondering why it is used in that situation, it is a shorthand for finding out if indexOf
method found something.
indexOf
returns -1 when it doesn't find something, and >= 0 when it does. So when you do ~-1 you get 0 (a falsy value) and when you do it on anything else you get a truthy value.
if( ~str.indexOf( "something" ) ) {
...
}
Is a shorter way of saying
if( str.indexOf( "something" ) !== -1 ) {
...
}
If you are wondering how is -1 the NOT of 0, then read here
Related Articles
What does '@@ DEBUG' mean in javascript?
& lt; & lt; - which means in Ruby
What do +++ and & mdash; means in JavaScript?
How do I know which version of Javascript I use?
What is I & lt; & lt; 1 mean in Javascript?
Windows Phone 8 SDK - This computer does not support hardware virtualization, which means that Windows Phone Emulator 8.0 can not run on this PC
WebView hides the software keyboard during loadUrl (), which means that a keyboard can not remain open during the javascript call
What does the @ symbol mean in javascript?
Which language to choose for the Windows 8 application (which means what are the implications)?
what is the meaning of javascript functions?
What does it mean by "JavaScript objects can be indexed by strings"?
What does this syntax mean in javascript?
What an exclamation point before a variable means in JavaScript
What does this mean in javascript?
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK