Type test

typeof

Can detect all basic data types and function types except null

var e = null; console.log(typeof(e)); Var f = [1,2,3]; console.log(typeof(f)); // object var g = {}; console.log(typeof(g)); // objectCopy the code

instanceof

Check whether the object’s prototype chain points to the constructor’s prototype object

How do extensions implement Instanceof

function instance_of(L, R) { var O = R.prototype; var L = L.__proto__; while (true) { if (L === null) return false; if (O === L) return true; L = L.__proto__; }}Copy the code

constructor

Primitive types, whether created by literals or constructors, can be detected. It can also detect Array, Object, and Function reference types, but not Null and Undefined

Object.prototype.toString

Determine which built-in type you belong to, all of which can be detected

Object.prototype.toString.call(null); // "[object Null]" Object.prototype.toString.call(undefined); // "[object Undefined]" Object.prototype.toString.call('abc'); // "[object String]" Object.prototype.toString.call(123); // "[object Number]" Object.prototype.toString.call(true); // "[object Boolean]" var date = new Date(); Object.prototype.toString.call(date); // "[object Date]"Copy the code

Type conversion

Expand the Object. The prototype. The valueOf ()

Returns the original valueOf the specified object, or if the object has no original value, valueOf returns the object itself.

Expand the Object. The prototype. The toString ()

Method returns a string representing the object. Note the conversions to arrays and functions

var x = {} console.log(x.toString()) // "[object Object]" toString.call(undefined) //"[object Undefined]" Tostring. call(null) // "[object null]" var x = [1,2, 3] x.tostring () // "1,2, 3" var x = function() {console.log('hello world')} x.toString() // "function(){console.log('hello world')}" var x = 12345 x.toString() // "12345" [null].toString() //"" [undefined].toString()//"" [].toString() //"" [""].toString() // ""Copy the code

Expand Number(),parseInt(), parseFloat()

  • Number()

    Can be used to convert any data type to a value

    • Object: is called firstThe valueOf () and toString ()methods
      A: Number ({1}) = > Number (' [object object] ') / / NaN Number ([1]) = > Number (' 1 ') / / 1 Number ([1, 2]) = > Number (' 1, 2) / / NaNCopy the code
    • String: ignore leading 0,The pure digitalThe string forNaN
    • undefuined: NaN
  • The parseInt (), the parseFloat ()

    Used specifically to convert strings to numeric values until all subsequent characters are parsed or a non-numeric character is encountered

Type conversions in mathematical operators

  • (- * /), will firstThe NumberType conversion toNumberType.
    1 * {a: 'qq'} // NaN 2 * ['5'] // NaN 2 * {a: 'qq'} // NaNCopy the code
  • Particularity of addition
    • When a side forStringType that is recognized as string concatenation and will preferentially convert the other side toString type.
    • When a side forNumberType and the other side isThe original type, the original type is converted toNumberType.
    • When a side forNumberType and the other side isReference types, will reference the type andNumberType to string concatenation.
      123 + '123' // 123123
      
      123 + null  // 123
      123 + true // 124
      
      123 + {}  // 123[object Object] 
      123+ [12] // "12312"
      []  // ''
      Copy the code

Type conversions in logical statements

  • (if || && while !) Logical operator

    Null undefined ‘NaN 0 false

  • == conversion rule

    • NaNAnd otherAny typeComparison always returnsfalse(including himself)
    • Boolean is first converted to the Number type in comparison to any other type.
    • StringandNumberCompare, first willStringconvertNumberType.
       '12' > 5     //true
       '12' > '5'   //false
      Copy the code
    • null == undefinedThe result of the comparison istrue
       null == undefined // true
       null == '' // false
       null == 0 // false
       null == false // false
       undefined == '' // false
       undefined == 0 // false
       undefined == false // false
      Copy the code
    • The original typeandReference typesReference types are compared according toToPrimitiveThe rule is converted to the original type.