1.” Back “button (history.back() can create a browser” back “button)
<button onclick="history.back()"> back < / button >Copy the code
2. Numeric delimiters (for readability)
const langerNumber = 1 _000_000_000;
console.log(langerNumber);/ / 1000000000
Copy the code
3. Event listeners run only once
el.addEventListener('click'.() = >console.log('I run only once'), {once:true})
Copy the code
4. Console. log(Variable wrapping)
const myNumber = 123;
console.log({myNumber});//{myNumber:123}
Copy the code
5. Get the minimum/maximum value from the array (math.min () or math.max ())
const numbers = [4.5.6.2.7];
console.log(Math.max(... numbers));/ / 7
console.log(Math.min(... numbers));/ / 2
Copy the code
6. Check whether Caps Lock is enabled
const passwordInput = document.getElementById('password');
passwordInput.addEventListener('keyup'.function(event){
if(event.getModifierState('CapsLock'))})Copy the code
7. Copy to clipboard
function copy(text){
navigator.clipboard.writeText(text)
}
Copy the code
8. Obtain the mouse position
document.addEventListener('mousemove'.(e) = >{
console.log(`x:${e.clienX},y:${e.clienY}`)})Copy the code
9. Shorten arrays
const numbers = [1.2.3.4.5]
numbers.length = 3;
console.log(numbers);/ / [1, 2, 3]
Copy the code
10. Short conditional statement
//
if(count){
doSoming();
}
/ / short
count && doSoming();
Copy the code
11. Array deduplication
const numbers = [2.3.4.4.2];
console.log([...new Set(numbers)]);/ / / 2 and 4
Copy the code
12. Convert strings to numbers
const str = '404';
console.log(+str);/ / 404
Copy the code
13. Convert numbers to strings
const num = 403;
console.log(num+' ');/ / '403'
Copy the code
14. Filter all virtual values from the array
const myArray = [1.undefined.NaN.2.null.'str'.true.3.false]
console.log(myArray.filter(Boolean));/ / [1, 2, 'STR', true, 3]
Copy the code
15.includes
const myTech = 'java';
const techs = ['html'.'css'.'java'];
console.log(techs.includes(myTech));
Copy the code
16. Sum over arrays (reduce)
const myArray = [10.20.30.40]
const reducer = (total,curren) = > total+curren
console.log(myArray.reduce(reducer));/ / 100
Copy the code