LoginSignup
7
6

More than 5 years have passed since last update.

JSの紛らわしい記法たち

Posted at

!value

var value;

value = {};
console.log(!value);
// false

value = undefined;
console.log(!value);
// true

value = null;
console.log(!value);
// true

value = 0;
console.log(!value);
// true

a || b

function out(a, b) {
  console.log(a || b);
}

out('aaa', 'bbb');
// aaa

out(undefined, 'bbb');
// bbb

out(null, 'bbb');
// bbb

out(null, null);
// null

out(0, 'bbb');
// bbb

a && b

function out(a, b) {
  console.log(a && b);
}

out('aaa', 'bbb');
// bbb

out(undefined, 'bbb');
// undefined

out(null, 'bbb');
// null

out(null, null);
// null

out(0, 'bbb');
// 0
7
6
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
6