JavaScript Kinky Tricks

Source: Internet
Author: User

1. Turn the rounding into a numerical type:

1     console.log ("10.340" |);    102     Console.log ("10.560" ^0);    103     Console.log ( -2.25|0);       -24     Console.log (~~-2.25);       -2

2. Date to Value:

    var d = +new Date ();        1499149610813

3. Random Code:

1 Math.random (). toString (+). substring (2);        14-bit: adfe8d8800e042 math.random (). ToString ($). substring (2);        11-bit: Yc290rtrpij

4. Exchange value:

1 a= [B, b=a][0];

5. Condition Judgment:

1     var a = b && 1; 2     //equivalent to 3     if (b) {4         a = 1    } 6  7     var a = B | | 1; 8     //equivalent to 9     if (b) A = b;10 else a = 1;    

6. Empty the array:

1     arr.length=0;        Set array length empty array

7. Force the Boolean value:

1 var a= ""; 2!! A;            Double exclamation points to really convert the variable to the corresponding Boolean value, the first exclamation point is converted to a Boolean value, but this operation gets its inverse value, and then take the inverse operation to get its corresponding true Boolean value

8. Application of the displacement character:

1 var num = >> 1; Equivalent to 10/2, but more efficient 2 console.log (num)//5;3 4 var num = 2 << 3; 2 of Four Square 5 console.log (num)//16;

JavaScript Kinky Tricks

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.