Type confusion

What is a function typeof?

+3
source share
5 answers

typeofit is actually an operator, not a function, it returns the type of the operand provided to you and always returns [1] a string value, which can be:

  • "undefined"
  • "object"
  • "boolean"
  • "number"
  • "string"
  • "function"

It may look like a function because it can be applied to expressions in brackets, but brackets are optional, you can use it without them:

typeof expression

, , ( , , ), ReferenceError, "undefined", :

if (typeof imUndeclared == 'undefined') {
  // Ok...
}

// Vs.

if (imUndeclared === undefined) { // ReferenceError!
}

, , , typeof "object" null, ...

, Array , RegExp, Date .., , .


[1] JScript (IE):), "unknown"!!! -, . typeof new ActiveXObject("Msxml2.XMLHTTP").abort; // "unknown"

+15

typeof

typeof ( ), ECMAScript , , JavaScript ; JavaScript 1.1, ECMA International.

(typeof expression) , string, (. ).

ECMAScript 5 (ECMA-262, . 71), :

  • Undefined → "undefined"
  • Null → "object"
  • Boolean → "boolean"
  • Number → "number"
  • string → "string"
  • Object → :
    • native Call → "object"
    • native Call → "function"
    • Call"undefined", "boolean", "number" "string"

typeof:

  • Array → "object"
  • Function → "" (. Object 2- 3- ...)

( , , , typeof, )

+6

typeof , .

+1

Of. , , Chris, . , , JavaScript. , , :

alert(typeOf([1,2,3]));  // Array
alert(typeOf(123));  // Number
alert(typeOf(/a_reg_exp/gi));  // RegExp
alert(typeOf(null));  // null
alert(typeOf(undefined));  // undefined

, , :

alert(typeOf([1,2,3], "Array"));  // true
alert(typeOf(123, "String"));  // false
alert(typeOf(/a_reg_exp/gi, "RegExp"));  // true
alert(typeOf(null, "undefined"));  // false
alert(typeOf(undefined, "undefined"));  // true
+1

( , , ..).

0

Source: https://habr.com/ru/post/1768942/


All Articles