From the JavaScript Reference on MDC,

~ (Bitwise NOT)

Performs the NOT operator on each bit. NOT a yields the inverted value (a.k.a. one’s complement) of a. The truth table for the NOT operation is:

a NOT a
0 1
1 0

Example:

9 = 00000000000000000000000000001001 (base 2)
--------------------------------
~9 = 11111111111111111111111111110110 (base 2) = -10 (base 10)

Bitwise NOTing any number x yields -(x + 1). For example, ~5 yields -6.

Now lets look at the Logical NOT(!)

! (Logical NOT)

Returns false if its single operand can be converted to true; otherwise, returns true.

Mixing the two NOT operators together can produce some interesting results:

!~(-2) = false

!~(-1) = true

!~(0) = false

!~(1) = false

!~(2) = false

For all integer operands except -1, the net operand after applying the ~ operator for the ! operator would be truthy in nature resulting in FALSE.

-1 is special because ~(-1) gives 0 which is falsy in JavaScript. Adding the ! operator gives us the only TRUE.

When to use this special case ?

A lot of times in JavaScript String manipulation, you are required to search for a particular character in a string. For example,

 var str = 'posterous';

 if ( str.search('t') >= 0 ) {
// character t found
}
else{
// not found
}

We can use the operators instead of the comparison operators, like this:

 var str = 'posterous';

 if ( !~str.search('t') ) {
// character 't' not found branch
}
else{
// found branch
}
原帖地址:http://www.javascriptturnsmeon.com/the-tilde-operator-in-javascript/

The tilde ( ~ ) operator in JavaScript的更多相关文章

  1. 【转】The && and || Operator in JavaScript

    原文: https://blog.mariusschulz.com/2016/05/25/the-andand-and-operator-in-javascript The && an ...

  2. What is the !! (not not) operator in JavaScript?

    What is the !! (not not) operator in JavaScript? 解答1 Coerces强制 oObject to boolean. If it was falsey ...

  3. [TypeScript] Use the JavaScript “in” operator for automatic type inference in TypeScript

    Sometimes we might want to make a function more generic by having it accept a union of different typ ...

  4. JavaScript简易教程(转)

    原文:http://www.cnblogs.com/yanhaijing/p/3685304.html 这是我所知道的最完整最简洁的JavaScript基础教程. 这篇文章带你尽快走进JavaScri ...

  5. JavaScript constructors, prototypes, and the `new` keyword

    Are you baffled(阻碍:使迷惑) by the new operator in JavaScript? Wonder what the difference between a func ...

  6. JavaScript简易教程

    这是我所知道的最完整最简洁的JavaScript基础教程. 这篇文章带你尽快走进JavaScript的世界——前提是你有一些编程经验的话.本文试图描述这门语言的最小子集.我给这个子集起名叫做“Java ...

  7. 【转】Expressions versus statements in JavaScript

    原文地址:http://www.2ality.com/2012/09/expressions-vs-statements.html Update 2012-09-21: New in Sect. 4: ...

  8. Kibana6.x.x源码分析--JavaScript中 "!~" 这样的符号是啥意思?

    看到源码中有一段JS代码不太懂,如下: 里面这个 "!~" 符号看到后有点儿方啊O__O "…,毛线意思? [查资料,解释如下]: indexOf returns -1 ...

  9. javascript prototype原型链的原理

    javascript prototype原型链的原理 说到prototype,就不得不先说下new的过程. 我们先看看这样一段代码: <script type="text/javasc ...

随机推荐

  1. rtsp/rtmp/hls/onvif测试源以及ffmpeg在流媒体方面的应用

    一.rtsp/rtmp/hls/onvif测试源 1. rtsp rtsp://184.72.239.149/vod/mp4:BigBuckBunny_175k.mov 2.rtmp rtmp://l ...

  2. matplotlib绘制散点图

    参考自Matplotlib Python 画图教程 (莫烦Python)(10)_演讲•公开课_科技_bilibili_哔哩哔哩 https://www.bilibili.com/video/av16 ...

  3. lua全局状态机

    本文内容基于版本:Lua 5.3.0 global_State概述 global_State结构,我们可以称之为Lua全局状态机.从Lua的使用者角度来看,global_State结构是完全感知不到的 ...

  4. [C语言]删除用户自定义后缀名的所有文件

    环境:win7 IDE:DEV-C++ 编译器:GCC 编译结果:Success 运行结果:Success 使用说明: 1.输入需要查询的目录,比如e: 2.输入需要删除的后缀名:比如:txt 注意: ...

  5. Dictionary 初始化数据

    Dictionary<string, string> dic = new Dictionary<string, string>() {                    { ...

  6. JQuery变量数字相加的研究

    在 jquery中,一个变量和一个数字相加,期望得到1+2=3但是: eg: <input type="text" class="input_Num" i ...

  7. hbase(二)

    一.HBase简介 1.1简介 hbase是bigtable的开源山寨版本.是建立的hdfs之上,提供高可靠性.高性能.列存储.可伸缩.实时读写的数据库系统.它介于nosql和RDBMS之间,仅能通过 ...

  8. mysql 及练习题

    if() 函数的用法 IF(expr1,expr2,expr3),如果expr1的值为true,则返回expr2的值,如果expr1的值为false, mysql,'女','男') as sex fr ...

  9. iOS 关于BTC 一些知识点

    1.BTC 用这个网 可以校验 自己的库生成的助记词 地址 是否是合法正常的 https://iancoleman.io/bip39/ 2.知晓 BTC 钱包是否有钱 和交易记录 https://te ...

  10. java.lang.OutOfMemoryError: PermGen space异常及解决

    PermGen space的全称是Permanent Generation space,是指内存的永久保存区域,这块内存主要是被JVM存放Class和Meta信息的,Class在被Loader时就会被 ...