JavascriptでBooleanからintへの型変換

intからBooleanへはよく変換するが、逆のパターンは知らなかったのでメモ。

これが一番いいかも。

var boolValue = true;
var intValue = +boolValue;
alert(intValue);// 1 が出力される
boolValue = false;
intValue = +boolValue;
alert(intValue);// 0 が出力される

参考にしたページ

Posted on 2011/11/16, in 開発 and tagged . Bookmark the permalink. Leave a Comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.