0

which means `~` in javascript

 2 years ago
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.
neoserver,ios ssh client

which means `~` in javascript

advertisements

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?

As the title puts, I really don't know what @@DEBUG or some other @@stringName mean in JavaScript I mean. Anybody got an idea there? Many thanks I've saw it used via string literal, or a variable, as follows: // the 1st scene angular.module('myApp',

& lt; & lt; - which means in Ruby

This question already has an answer here: What does <<-CONSTANT do? 3 answers What does <<- mean in the following method/context? class_eval <<-RUBY, __FILE__, __LINE__ + 1 def destroy_associations association(:#{middle_reflection.name})

What do +++ and & mdash; means in JavaScript?

My work is based on JSLint. I saw that JSLint is able to handle +++ and --- operator. I searched these operator on Internet but found nothing. What do these two operators mean in JavaScript?There is no such operator. +++ is parsed as ++ +. Writing co

How do I know which version of Javascript I use?

I'm just reading this documentation about Javascript 1.2, but I'm wondering which version of Javascript is being used in the most popular browsers. http://www.tutorialspoint.com/javascript/javascript_nested_functions.htmWikipedia (or rather, the comm

What is I & lt; & lt; 1 mean in Javascript?

This question already has an answer here: What do ">>" and "<<" mean in Javascript? 10 answers What do the << and >> operator do? [duplicate] 2 answers I was going through some code for prime numbers on Stack an

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

Possible Duplicate: Unable to create the virtual machine Warning during installation of WP8 SDK: "This computer does not support hardware virtualization, which means Windows Phone Emulator 8.0 can't run on this PC." Environment: Windows 8 Enterp

WebView hides the software keyboard during loadUrl (), which means that a keyboard can not remain open during the javascript call

Since the way you call javascript on a WebView is through loadUrl("javascript: ... "); The keyboard cannot stay open. The loadUrl() method calls loadUrlImpl() , which calls a method called clearHelpers() which then calls clearTextEntry(), which

What does the @ symbol mean in javascript?

For example: import Component from '@/components/component' In the code I'm looking at it behaves like ../ going up one level in the directory relative to the file path, but I'd like to know more generally what it does. Unfortunately I can't find any

Which language to choose for the Windows 8 application (which means what are the implications)?

Hi I'm going to give windows 8 app development a go, as I'm trying to return to the windows platform, and I really like the prospects of the Surface Pro, so I'm wondering what programming language I should use? I'm primarily a web developer, and I al

what is the meaning of javascript functions?

function ExChgClsName(Obj,NameA,NameB){ var Obj=document.getElementById(Obj)?document.getElementById(Obj):Obj; Obj.className=Obj.className==NameA?NameB:NameA; } <a href="javascript:showMenu(2);"> i am a newbie of the js. so can't understan

What does it mean by "JavaScript objects can be indexed by strings"?

var people = new Array(); function People (name, location, age){ this.name = name; this.location = location; this.age = age; } I have two other functions to generate the people and load them into a table. function generatePeople(){} function loadPeop

What does this syntax mean in javascript?

;(function(Register, $, undefined) { 'use strict'; Register.Model = { Uid: '' }; Register.Handler= { init: function() { Register.Model.Uid= $('body').data('uid'); } }; })(window.Register= window.Register|| {}, jQuery); Hello, I'm new to javascript an

What an exclamation point before a variable means in JavaScript

I'm trying to learn JavaScript by going through some code in an application and I keep seeing !variable in if conditions. For example: if (!variable.onsubmit || (variable.onsubmit() != false)) { What is it? Some kind of test if the variable is empty?

What does this mean in javascript?

If I just put a 'this' at the beginning of JavaScript, it's not in any functions. Does 'this' have the same meaning with 'document'? or it means window? Example: $(this).ajaxComplete(handler); In this case, do I attache the handler to the window or t

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK