📜 ⬆️ ⬇️

Js tips

Algorithm for getting result A && B

If operand A is true (true, "string", someObj), the result is B, otherwise - A.
I mean the code:

if (a) {
return a.member;
} else {
return a;
}


Can be replaced by a more compact one:
')
return a && a.member;

Algorithm getting the result A || B

If operand A is true, the result is A, otherwise - B.
This feature can be used to assign default values ​​to variables:
last = input || default_value;

Creating an object on the fly

obj = {fieldName: "a"}; - object with fieldName

Source: https://habr.com/ru/post/48387/


All Articles