Some typical Javascript code

Source: Internet
Author: User

1. convert an integer to a numeric value at the same time:
'10. 567890 '| 0
Result: 10
'10. 567890 '^ 0
Result: 10
-2.23456789 | 0
Result:-2
~~ -2.23456789
Result:-2
2. date to numeric value:
Var d = + new Date (); // 1295698416792
3. convert a class array object to an array:
Var arr = []. slice. call (arguments)
4. Pretty random code:
Math. random (). toString (16). substring (2); // 14 digits
Math. random (). toString (36). substring (2); // 11 digits
5. Merge Arrays:
Var a = [1, 2, 3];
Var B = [4, 5, 6];
Array. prototype. push. apply (a, B );
Uneval (a); // [1, 2, 3, 4, 5, 6]
6. Use 0 to complete the full digits:
Function prefixInteger (num, length ){
Return (num/Math. pow (10, length). toFixed (length). substr (2 );
}
7. exchange value:
A = [B, B = a] [0];
8. insert an array to the specified position of another array:
Var a = [1, 2, 3, 7, 8, 9];
Var B = [4, 5, 6];
Var insertIndex = 3;
A. splice. apply (a, Array. concat (insertIndex, 0, B ));
// A: 1, 2, 3, 4, 5, 6, 7, 8, 9
9. Delete array elements:
Var a = [1, 2, 3, 4, 5];
A. splice (3, 1 );
10. quickly obtain the maximum and minimum values of the array.
Math. max. apply (Math, [1, 2, 3]) // 3
Math. min. apply (Math, [1, 2, 3]) // 1
(From http://ejohn.org/blog/fast-javascript-maxmin)
11. Condition judgment:
Var a = B & 1;
Equivalent
If (B ){
A = 1
}
Var a = B | 1;
Equivalent
If (B ){
A = B;
} Else {
A = 1;
}
12. Judge IE:
Var ie =/* @ cc_on! @ */False;
What else? Welcome to respond
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.