Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
0
Help us understand the problem. What is going on with this article?
@okaponta_

lodashのtoNumberの戻り値まとめ

lodashtoNumberの仕様がよく分からなくなったので、いろいろ実験してみました。
結論から言うと、undefinedの時だけ注意が必要そうです。

引数 引数(コード) 結果
文字の'0' '0' 0
空文字 '' 0
半角スペース ' ' 0
全角スペース ' ' 0
null null 0
undefined undefined NaN
数字ではない文字 'hoge' NaN

おまけ

以下で確認しました。

console.log('toNumber');
let vari = '0';
console.log(vari + '->' + _.toNumber(vari));
vari = '';
console.log(vari + '->' + _.toNumber(vari));
vari = ' ';
console.log(vari + '->' + _.toNumber(vari));
vari = ' ';
console.log(vari + '->' + _.toNumber(vari));
vari = null;
console.log(vari + '->' + _.toNumber(vari));
vari = undefined;
console.log(vari + '->' + _.toNumber(vari));
vari = 'hoge';
console.log(vari + '->' + _.toNumber(vari));
0
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
okaponta_
twitter @okaponta_ フォロー待ってます!

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
0
Help us understand the problem. What is going on with this article?