0
0

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 1 year has passed since last update.

【TypeScript】条件判定結果

Last updated at Posted at 2022-06-13

はじめに

if文や三項条件式の条件を書く時に条件判定がどうだったか?たまに忘れるためまとめました。
※2022年7月16日に参考文献を追記しました。そちらに詳細な文献あります。

結論

type value result
number -1 true
number 1 true
number 0 false
string "" false
string "hoge" true
boolean true true
boolean false false
any[] [] true
{} {} true
any null false
any undefined false

詳細

※今回は三項条件式で書いてます。

const number1  : number  = -1;
const number2  : number  = 1;
const number3  : number  = 0;
const text1    : string  = "";
const text2    : string  = "hoge";
const boolean1 : boolean = true; // 真偽値
const boolean2 : boolean = false; // 真偽値
const array    : any[]   = new Array();
const object   : {}      = {};
const other1   : any     = null;
const other2   : any     = undefined;

var test = number1? "true" : "false";
console.log(test); // true

var test = number2? "true" : "false";
console.log(test); // true

var test = number3? "true" : "false";
console.log(test); // false

var test = text1? "true" : "false";
console.log(test); // false

var test = text2? "true" : "false";
console.log(test); // true

var test = boolean1? "true" : "false";
console.log(test); // true

var test = boolean2? "true" : "false";
console.log(test); // false

var test = array? "true" : "false";
console.log(test); // true

var test = object? "true" : "false";
console.log(test); // true

var test = other1? "true" : "false";
console.log(test); // false

var test = other2? "true" : "false";
console.log(test); // false

参考文献

●追記 2022年7月16日
こちらの文献ですが、コメント欄で@shiracamusさんに教えて頂いた文献になります。
mdn web docs:Falsy (偽値)
mdn web docs:Truthy (真値)

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?