判断undefined

  1. function judgeUndefined (arg) {
  2. if ( typeof arg === 'undefined') {
  3. console.log('undefined')
  4. }
  5. }
  6. judgeUndefined(undefined)

判断null

法一

  1. function judgeNull (arg) {
  2. if ( !arg && typeof arg !== 'undefined' && arg != 0) {
  3. console.log('null')
  4. }
  5. }
  6. judgeNull(null)

法二

  1. function getType (obj) {
  2. let type = Object.prototype.toString.call(obj)
  3. return type.split(' ')[1].replace(']', '')
  4. }
  5. console.log(getType([])) // Array
  6. console.log(getType(null)) // Null