Javascript Tricks
Javascript Tricks
Description 😋
This is a collection of JavaScript tips and tricks. you can refer to it and apply it
to make your code more concise. But don’t overdo it, it can make your code
difficult to read and maintain. Hope everyone contributes, thanks.
Table Of Content
• Description
• Table Of Content
• Array
• Object
• Destructuring
• Operator
• Comparison
• Others
Array
1. Generate an Array
• Create an empty array of length n
/*
result: arr = [1, 1, 1]
arr2 = [1, 1, 1, undefined, undefined]
*/
/*
result: arr = [1, 2, 3, 4]
arr2 = [1, 2, 3, 4]
arr3 = [0, 2, 4, 6]
arr4 = [0.211, 0.5123, 0.612, 0.8921]
*/
// result: [1, 2, 3, 4, 5, 6, 7]
1. Resize an Array
The length array isn’t a read only property.
/*
result: arr = [1, 2]
arr2 = []
arr3 = [1, 2, 3, 4, 5, undefined, undefined]
*/
// Not Recommended
delete arr[1]; // arr = [1, undefined, 3], length = 3
// Recommended
arr.splice(1, 1); // arr = [1, 3], length = 2
Object
1. Dynamic Property Name
var obj = {
name: 'Dyno',
[dynamic]: dynamicValue,
};
1. Clone an Object
• Shallow copy (Not Recommended)
Use the = operator to copy object 1 into object 2. These 2 objects point to the
same memory area (reference). Therefore, if we change object 1, object 2
will also change.
var obj1 = { a: 1, b: 2 };
var obj2 = obj1; // obj2 = { a: 1, b: 2 }
• Deep copy
Way 1: Use Spread operator {...} or Object.assign() to fix “Shallow copy”.
Issue: Nested objects still have shallow copy problem.
obj1.b = 3;
obj1.c.nested = 4;
console.log(obj1); // { a: 1, b: 3, c: { nested: 4 } }
console.log(obj2); // { a: 1, b: 2, c: { nested: 4 } }
console.log(obj3); // { a: 1, b: 2, c: { nested: 4 } }
obj1.b = 3;
obj1.c.nested = 4;
console.log(obj1); // { a: 1, b: 3, c: { nested: 4 } }
console.log(obj2); // { a: 1, b: 2, c: { nested: 3 } } 😉😘
Destructuring (ES6+)
1. With Array
1. With Object
Operator
1. Optional chaining (?.)
“The optional chaining operator ?. enables you to read the value of a property
located deep within a chain of connected objects without having to check that
each reference in the chain is valid.” MDN
const person = {
name: 'Dyno',
age: 18,
sayHello: function () {
console.log('Hello');
},
};
// Wrong way
console.log(person.infor.address); // Uncaught TypeError: Cannot
read property 'address' of undefined
// syntax
obj.val?.prop;
obj.val?.[expr];
obj.arr?.[index];
obj.func?.(args);
1. Logical OR (||)
var a = 1,
b = 2;
// result: Dyno
let a = true,
b = true,
c = false;
// other usage
function sayHi() {
console.log('Hi');
}
a && sayHi(); // Hi
c && sayHi(); // false
a ||= b; // same a = a || b;
a ??= b; // same a = a ?? b;
Comparison
1. Use === instead of ==
The operator == (!=) will automatically cast if 2 variables are not of the same
type, then compare. The === (!==) operator compares the value and the type
=> === faster than ==.
1 == '1' // true
1 === '1' // false
0 == false // true
0 === false // false
[] == 0 // true
[] === 0 // false
isNaN('string');
// true, 'string' is not Number
isNaN([]);
// true, [] is not Number
isNaN(0 / 0);
// true, 0/0 is not Number
isNaN(1);
// false, 1 is Number
Number.isNaN('string');
// false, 'string' is not NaN
Number.isNaN([]);
// false, [] is not NaN
Number.isNaN(0 / 0);
// true, 0/0 is NaN
Number.isNaN(NaN);
// true
Others
1. Swapping use Destructuring
let a = 1,
b = 2;
// result: a = 2, b = 1;
/*
same: const fn = () => {
return { obj: 1 }
}
*/
// Way 2:
var res = (() => {
console.log('Hello');
return true;
})();
// result: Hello, res = true;
1. typeof vs instanceof
typeof 1; // "number"
typeof NaN; // "number"
typeof 'str'; // "string"
typeof true; // "boolean"
typeof {}; // "object"
typeof []; // "object"
typeof null; // "object"
typeof undefined; // "undefined"
typeof function name() {}; // "function"
1. Falsy
console.log(!!null); // false
console.log(!!undefined); // false
console.log(!!1); // true