1. use !! Convert a variable to a boolean type

Sometimes we need to check whether some variables exist , Or does it have a valid value , So their values are treated as true. For doing this kind of inspection , You can use it !!( Double negative operator ), It can automatically convert any type of data to Boolean , Only then will the variable return false:0,null,”“,undefined or NaN, All other values are returned true.
2. use + Replace variables with numbers
This is super easy to replace , But she only applies to numeric strings , Or it will return NaN( It's not a number ). Look at this example :
function toNumber(strNumber){
return +strNumber;
This replacement operation can also be used on date, In this case , It will return the timestamp :
console.log(+ new Date());//1462548741
3. use || Set default value
stay ES6 There is a default parameter function in . To simulate this feature in older browsers , You can use it ||
(or operator ), And take the default value as its second parameter . If the first parameter returns false, The second parameter will be returned as the default value . Look at the following example :
function User(name,age){
this.name = name || “David”;
this.age = age || “25”;
var user1 = new User();
var user2= new User(“Barry”,”10”):
4. Cache in loop array.length
var length = array.length;
for(var i = 0; i < length; i++){
For simplicity , It can be written like this
for(var i = 0,length = array.length; i < length; i++){
5. Gets the last element of the array

Array.prototype.slice(begin,end) Can be used to crop arrays . But if the end parameter is not set , This function automatically sets the end Set to array length value . If you will begin If it is set to a negative number , You can get the countdown from the array .
var array = [1,2,3,4,5];
6. Array truncation

This technique can lock the size of the array , This is useful for removing a fixed number of elements from an array . for example : The inclusion of an array 10 Elements , But you just want to take out the first five values , You can set the array.length
= 5 To truncate the array . example :
var array = [1,2,3,4,5,6];
array.length = 3;
7. replace all
var str = “abacc”;
8. Merge array
If you need to merge two arrays , This can be done :
var arr1 = [1.2];
var arr2 = [3,4];
9. hold NodeList Convert to array
Just use this function :[].slice.call(elements);
10. Shuffle array elements
var list = [1,2,3];
return Math.random - 0.5

©2019-2020 Toolsou All rights reserved,
Final review of database : Summary of comprehensive application questions Laplance operator ( Second derivative ) Simple learning of computer composition principle pyqt Button call python program _PyQt: Link button to function in program How much can you go up once you change jobs ? Today, I saw the ceiling of job hopping python in str Function usage _python in str Usage Summary of built-in functions MySQL trigger web The server nginx---linux Installation and deployment C++ Chapter V polymorphism exercises :( It's coming to an end )python Check built-in functions , How to check python Built in function