JS by-bit non (~) Operators and ~~ Understanding and Analysis of operators _ javascript skills

Source: Internet
Author: User
Bitwise non-operators, a simple understanding is to change the symbol of the number of operations and subtract 1. Of course, this is just a simple understanding of converting data to the number type. For typeof var! = "Number" type, during the operation, it will try to convert to 32-bit integer data. If it cannot be converted to integer data, it will be converted to NaN;
Javascript in-place operations use a simpler method to implement this operation, so its implementation principle can be understood as follows:

The Code is as follows:


Var testData =-2.9;
Var testResult = (typeof testData = "number "&&! IsNaN (testData) & testData! = Infinity )? (TestData> 0 )? -Math. floor (testData)-1:-Math. ceil (testData)-1:-1;


First, if a data is converted to 32 integer data and the result is <0, it needs to be rounded up, for example,-2.9->-2. If> 0, for example, 2.6-> 2;
If a data cannot be converted to a 32-bit binary representation, it is converted to NaN, and then to-1. For example ~ {}/~ NaN =-1;
For example ~ Function () {return 100;}->-1;
In Jquery, such as if (!~ This. className. indexOf (str) {// do some thing .....}; Here, the returned value of this. className. indexOf (str) is either greater than-1 or equal to-1. When it is equal to-1 ,~ -1 = 0; then ,!~ -1 = true; then we can conclude that this does not contain the str class name ...;
For ~~ Operator. Similarly, it can also be expressed:

The Code is as follows:


Var testData = 2.1;
Var testResult = (typeof testData = "number "&&! IsNaN (testData) & testData! = Infinity )? (TestData> 0 )? Math. floor (testData): Math. ceil (testData): 0;


We also use the round-robin mode;
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.