The tilde ( ~ ) operator in JavaScript
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) ofa
. 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
}
The tilde ( ~ ) operator in JavaScript的更多相关文章
- 【转】The && and || Operator in JavaScript
原文: https://blog.mariusschulz.com/2016/05/25/the-andand-and-operator-in-javascript The && an ...
- 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 ...
- [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 ...
- JavaScript简易教程(转)
原文:http://www.cnblogs.com/yanhaijing/p/3685304.html 这是我所知道的最完整最简洁的JavaScript基础教程. 这篇文章带你尽快走进JavaScri ...
- JavaScript constructors, prototypes, and the `new` keyword
Are you baffled(阻碍:使迷惑) by the new operator in JavaScript? Wonder what the difference between a func ...
- JavaScript简易教程
这是我所知道的最完整最简洁的JavaScript基础教程. 这篇文章带你尽快走进JavaScript的世界——前提是你有一些编程经验的话.本文试图描述这门语言的最小子集.我给这个子集起名叫做“Java ...
- 【转】Expressions versus statements in JavaScript
原文地址:http://www.2ality.com/2012/09/expressions-vs-statements.html Update 2012-09-21: New in Sect. 4: ...
- Kibana6.x.x源码分析--JavaScript中 "!~" 这样的符号是啥意思?
看到源码中有一段JS代码不太懂,如下: 里面这个 "!~" 符号看到后有点儿方啊O__O "…,毛线意思? [查资料,解释如下]: indexOf returns -1 ...
- javascript prototype原型链的原理
javascript prototype原型链的原理 说到prototype,就不得不先说下new的过程. 我们先看看这样一段代码: <script type="text/javasc ...
随机推荐
- Spark Sql的UDF和UDAF函数
Spark Sql提供了丰富的内置函数供猿友们使用,辣为何还要用户自定义函数呢?实际的业务场景可能很复杂,内置函数hold不住,所以spark sql提供了可扩展的内置函数接口:哥们,你的业务太变态了 ...
- HDU中大数实现的题目,持续更新(JAVA实现)
HDU1002:大数加法,PE了N次 import java.util.Scanner; import java.math.*; public class Main { public static v ...
- docker——Dockerfile(一)
Dockerfile是一个文本格式的配置文件,用户可以使用Dockerfile来快速创建自定义的镜像.Dockerfile由一行行命令语句组成,并支持以#开头的注释行.一般而言,Dockerfile分 ...
- 微信公众号JSAPI支付
微信公众号JSAPI支付 一:配置参数 申请成功后,获取接口文件, 将所有文件放入项目根目录weixin下,在WxPay.ub.config.php中填入配置账户信息; 二:设置授权 开发者中心-&g ...
- netty9---使用编码解码器
客户端: package com.client; import java.net.InetSocketAddress; import java.util.Scanner; import java.ut ...
- GZFramework错误(升级修改)日志
sqlserver下事务中处理出现为初始化selectcommand的connection属性修改CommandDataBase中的PrepareCommand方法
- handle 和module
<httpHandlers> <add verb="*" path="*" type="ClassLibrary831.TestHa ...
- 20135302魏静静——linux课程第三周实验及总结
linux课程第三周实验及总结 一.实验:跟踪分析Linux内核的启动过程 使用gdb跟踪调试内核从start_kernel到init进程启动 使用实验楼的虚拟机打开shell cd LinuxKer ...
- Swift进阶之路(一)——单例模式、属性传值、代理传值、闭包传值
一.单例模式 单例模式是设计模式中最简单的一种,甚至有些模式大师都不称其为模式,称其为一种实现技巧,因为设计模式讲究对象之间的关系的抽象,而单例模式只有自己一个对象. 关于单例,有三个重要的准则需要牢 ...
- [Deep Learning] 神经网络基础【转】
本文转载自:http://www.cnblogs.com/maybe2030/p/5597716.html 阅读目录 1. 神经元模型 2. 感知机和神经网络 3. 误差逆传播算法 4. 常见的神经网 ...