Dev. To/Redbossrabb… dev. To/Redbossrabb… Zero and zero
1. Assign values to variables using ternary expressions
❌ a > b? foo ='apple' : foo = 'ball'; ✅ foo = a > b?'apple' : 'ball';
Copy the code
2. Assign the same value to different attributes of the object based on conditions
❌ c > d? a.foo ='apple' : a.bar = 'apple'; ✅ a = {[c > d?'foo' : 'bar'] :'apple' };
Copy the code
3. Export multiple variables
❌
export const foo;
export const bar;
export const kip;
✅
export const foo, bar, kip;
Copy the code
4. Assign values to variables using attributes of the object
❌
const a = foo.x, b = foo.y;
✅
const{['x']: a, ['y']: b } = foo;
Copy the code
5. Assign a value to a variable using an array index
❌
let a = foo[0], b = foo[1];
✅
let [a, b] = foo;
Copy the code
6. Get multiple elements in the DOM
❌
const a = document.getElementById('a'),
b = document.getElementById('b'),
c = document.getElementById('c'),
d = document.getElementById('d');
✅
const elements = {};
['a'.'b'.'c'.'d'].forEach(item= >elements = { ... elements, [item]:document.getElementById(item)
});
const { a, b, c, d } = elements;
/* For the code to work properly, your element ID must be a valid JavaScript variable name and the variable name storing the element must match the element ID name */
Copy the code
7. Replace simple if statements with logical operators
❌
if (foo) {
doSomething();
}
✅
foo && doSomething();
Copy the code
8. Pass parameters based on conditions
❌
if(! foo) { foo ='apple'; } bar(foo, kip); ✅ bar (foo | |'apple', kip);
Copy the code
9. Deal with lots of zeros
❌
const SALARY = 150000000,
TAX = 15000000;
✅
const SALARY = 15e7,
TAX = 15e6;
Copy the code
10. Assign the same value to multiple variables
❌
a = d;
b = d;
c = d;
✅
a = b = c = d;
Copy the code
eggs
Using object structure assignments saves you the trouble of writing console.log() many times.
const{['log']: c } = console;
c('something');
Copy the code
Now you can debug code quickly using only C ().
(after)
Your likes will make my day, if it goes wellFor a starIt’s even more perfect.