LoginSignup
1
0

More than 3 years have passed since last update.

型について

Last updated at Posted at 2020-01-26

たまに苦しむ型について改めて調べてみました。

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"

やってみた
スクリーンショット 2020-01-26 12.50.15.png

new String と new Number()

new Stringnew Numberいう書き方もあります

const testStr = new String('test')
const testNum = new Number(1)

一見、string型とNumber型を作っている感じですが、違います.

スクリーンショット 2020-01-26 13.10.25.png

他にも多くに型がありますが、よく使うもの書いてみました。
みてわかるようにobject型はなんでもありな感じなので、型定義をるする時はなるべく使わない方がよいです。

参考
https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference
https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Date

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