nitroge / memories

日常学习累积的点点滴滴滴滴点点
7 stars 0 forks source link

何时以及如何在javascript里面使用Boolean? #14

Open nitroge opened 3 years ago

nitroge commented 3 years ago
let x = 'some-value';

// This doesn't look too nice
if (!!x) {
  // ...
}

// This is a lot more readable
if (Boolean(x)) {
  // ...
}
const values = [0, 0, 2, 0, 3];
// Use as the callback for Array.prototype.some()
const hasValidValue = values.some(Boolean);
// Use as the callback for Array.prototype.filter()
const nonEmptyValues = values.filter(Boolean);