5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

unless valとunless val?の違い

5
Last updated at Posted at 2013-05-07

unless val は、空文字列、0, false, null, undefined の場合に true になる。一方 unless val?null, undefined の場合に true になる。関数の必須引数チェックのときは throw new Error('') unless arg? とすることにする。

テストスクリプト

vals = ['', '0', 0, 0.0, false, 'false', null, 'null', undefined, 'undefined', [], {}]

console.log ' ', '\t', '?', '\t', 'value'
for val in vals
    console.log(
        (unless val  then 'x' else 'o'),
        '\t',
        (unless val? then 'x' else 'o'),
        '\t',
        JSON.stringify(val)
    )

結果

 	 ?	 value
x 	 o 	 ""
o 	 o 	 "0"
x 	 o 	 0
x 	 o 	 0
x 	 o 	 false
o 	 o 	 "false"
x 	 x 	 null
o 	 o 	 "null"
x 	 x 	 undefined
o 	 o 	 "undefined"
o 	 o 	 []
o 	 o 	 {}
5
5
0

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
5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?