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

【JavaScript】typeof演算子とは

Posted at

typeof演算子とは

typeof演算子は値のデータ型を返す演算子です。

組み込み型(プリミティブ型)の場合

window.onload = function () {
    // typeofの使用例

    // Number
    console.log( 'typeof 1 = ' + (typeof 1) );
    console.log( 'typeof 1.41 = ' + (typeof 1.41) );
    console.log( 'typeof Math.PI = ' + (typeof Math.PI) );
    console.log( 'typeof Infitity = ' + (typeof Infinity) );

    // String
    console.log( 'typeof "" = ' + (typeof "") );
    console.log( 'typeof "abc" = ' + (typeof "abc") );
    console.log( 'typeof "123" = ' + (typeof "123") );
    console.log( 'typeof (typeof 0) = ' + (typeof (typeof 0)) );

    // Boolean
    console.log( 'typeof true = ' + (typeof true) );
    console.log( 'typeof false = ' + (typeof false) );

    // Undefined
    console.log( 'typeof undefined = ' + (typeof undefined) );
}

結果

// Number
typeof 1 = number
typeof 1.41 = number
typeof Math.PI = number
typeof Infitity = number

// String
typeof "" = string
typeof "abc" = string
typeof "123" = string
typeof (typeof 0) = string

// Boolean
typeof true = boolean
typeof false = boolean

// Undefined
typeof undefined = undefined

関数やオブジェクト型の場合

window.onload = function () {
    // Function
    console.log( 'typeof function () { } = ' + (typeof function () { }) );
    console.log( 'typeof class abc { } = ' + (typeof class abc { }) );
    console.log( 'typeof Math.abs = ' + (typeof Math.abs) );

    // Object
    console.log( 'typeof { prop: 1 } =' + (typeof { prop: 1 }) );

    // nullはObject型です。
    console.log( 'typeof null = ' + (typeof null) );

    // Date()やString()のラッパーオブジェクトはObjectと判定されます。
    console.log( 'typeof new String("abc") = ' + (typeof new String("abc")) );

    // Array は Objectと判定されます。
    console.log( 'typeof [1, 2, 3] = ' + (typeof [1, 2, 3]) );

    // Array.isArray や Object.prototype.toString.call などで
    // 普通のオブジェクトと配列を区別できます。
    console.log( 'Array.isArray({ prop: 1 }) = ' + (Array.isArray({ prop: 1 })) );
    console.log( 'Array.isArray([1, 2, 3]) = ' + Array.isArray([1, 2, 3]) );
}

結果

// Function
typeof function () { } = function
typeof class abc { } = function
typeof Math.abs = function

// Object
typeof { prop: 1 } =object

// nullはObject型です。
typeof null = object

// Date()やString()のラッパーオブジェクトはObjectと判定されます。
typeof new String("abc") = object

// Array は Objectと判定されます。
typeof [1, 2, 3] = object

// Array.isArray や Object.prototype.toString.call などで
// 普通のオブジェクトと配列を区別できます。
Array.isArray({ prop: 1 }) = false
Array.isArray([1, 2, 3]) = true
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?