たまに苦しむ型について改めて調べてみました。
JavaScriptの基本的な型
| 型名 | 例 | 判定で表示される値 |
|---|---|---|
| Null型 | null | object |
| Undefined型 | null | undefined |
| 他のオブジェクト | {a: 1} | object |
| 数値型 | 1 | number |
| 文字列型 | abc | string |
| 真偽値型 | true / false | boolean |
| 関数 | function() {} | function |
型を判定する typeofの使い方
typeof "123" // "string"
typeof 123 // "number"
typeof {} // "object"
new String と new Number()
new Stringとnew Numberいう書き方もあります
const testStr = new String('test')
const testNum = new Number(1)
一見、string型とNumber型を作っている感じですが、違います.
他にも多くに型がありますが、よく使うもの書いてみました。
みてわかるようにobject型はなんでもありな感じなので、型定義をるする時はなるべく使わない方がよいです。
参考
https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference
https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Date
