- Convert string to Numeric
General Method:
var var1 = parseint ("2"), var var2 = parsefloat ("2"), var var3 = number ("2"), var var3 = new Number ("2");
Simple method:
var var1 = + ("2");
- Converting other types to Boolean types
In JavaScript, all values can be implicitly converted to a Boolean type:
Data type |
The value converted to true |
Value converted to False |
Boolean |
True |
False |
String |
Any non-empty string |
"" (empty string) |
Number |
Any non-0 numeric value (including infinity) |
0, NaN |
Object |
Any object |
Null |
Undefined |
(Not applicable) |
Undefined |
Example:
0 = = false; True1 = = true; True ' = = false//Truenull = = false//True
We can also display the conversion to a Boolean type:
var a = Boolean ("Hello"); True
A simpler approach:
var a = "Hello"; var b =!! A
- Creating multidimensional arrays
General Method:
var arr = new Array (2); arr[0] = new Array (2); arr[1] = new Array (2); arr[0][0] = 1;arr[0][1] = 2;arr[1][0] = 3;arr[1][1] = 4 ;
Simple method:
var arr = {};arr[[0, 0]] = 1;arr[[0, 1]] = 2;arr[[1, 0]] = 3;arr[[1, 1]] = 4;
- Prevent others from loading your page in an IFRAME
Prevent your Web page from embedding its own web pages through an iframe:
if (top!== window) { top.location.href = window.location.href;}
This code should be placed in the head of each page.
- Convert an arguments parameter object to an array
In JavaScript, the predefined variable arguments in a function is not a true array, but an array-like object. It has the length property, but there are no slice, push, sort functions for the array object, and these are sometimes used in functions, so we need to convert the arguments to a real array:
function func () { var arr = Array.prototype.slice.call (arguments, 0); return arr;}
- Traverse the resulting regular result
General Method:
var str = "132ada5d6g3j"; var match = Str.match (/\d/g, str); var arr = [];var j;for (var i = 0, j = match.length; I < J; I + +) { Arr.push (match[i]);} Console.log (arr);
Quick Method:
var str = "132ada5d6g3j"; var arr = []; Str.replace (/\d/g, function () { arr.push (arguments[0]);}); Console.log (arr);
Another quick way:
var str = "132ada5d6g3j"; var arr = str.replace (/\d/g, "). Split ('); Console.log (arr);
- Get the maximum value in a numeric array
General Method:
var arr = [1, 5, 4, 12355, 123, 123, 3, 4454, 43];var max = Arr[0];for (var i in arr) { if (Arr[i] > max) { m AX = arr[i];} } The/* Loop may also be: for (var i = 0, j = arr.length; I < J; i++) { if (Arr[i] > max) { max = arr[i];} } */console.log (max);
Simple method:
var arr = [1, 5, 4, 12355, 123, 123, 3, 4454, 43];var max = Math.max.apply (null, arr); Console.log (max);
General Method:
var num = 5.63;console.log (Math.floor (num));
Other Method 1 (only available for numbers not less than 0):
var num = 5.63;console.log (num>>>0);
Other Method 2 (positive negative numbers are available, recommended):
var num = 5.63;console.log (~~num);
- Fix An error similar to 0.1+0.2! = 0.3
In JavaScript, numbers are based on IEEE754 values, so there is a case of floating-point arithmetic errors, which is a common problem with IEEE754, not the language itself:
var num = 0.1 + 0.2; 0.30000000000000004console.log (num = = 0.3); False
Correction Method:
var num = 0.1 + 0.2; 0.30000000000000004console.log (num);/* You can specify the number of decimal digits rounded by the ToFixed method: */console.log (Num.tofixed ()); "0" Console.log (num.tofixed (1)); "0.3"
Common methods:
/* Note that the n here indicates the number of digits after Num 0 is */function addzero (num, n) { var len = num.tostring (). length; while (Len < n) { num = "0" + num; len++; } return num;} Console.log (Addzero (5,8)); 00000005
Simple method:
function Addzero (num, n) { y= ' 00000000000000000000000000000 ' +num;/* Here 0 of the number can be adjusted * /return Y.substr (Y.LENGTH-N) ;} Console.log (Addzero (5,8)); 00000005
——————————————————————-
The performance issues of string connections should be classic, not detailed.
Article Source: http://i.cnblogs.com/EditPosts.aspx?opt=1