Summary of Javascript typeof usage

  • 2020-06-12 08:28:15
  • OfStack

Arrays are used in js, such as multiple input with the same name. If they are generated dynamically, you need to determine if they are an array at commit time. = "undefined") {} This usage is incorrect.
Correct is if(typeof(document.mylist.length)! {} = "undefined")
Or if (! isNaN (document. mylist. length)) {}

The operand of typeof is undefined, and the return is "undefined".
The number typeof(x) = "number"
The string typeof(x) = "string"
Boolean VALUE typeof(x) = "boolean"
Object, array and null typeof(x) = "object"
Function typeof(x) = "function"

The typeof operator returns a string representing the data type of the expression.
Possible strings are "number", "string", "boolean", "object", "function" and "undefined".

Such as:
alert (typeof (123)); / / typeof (123) returned number ""
alert (typeof (" 123 ")); / / typeof (" 123 ") returns "string"

The typeof operator returns a string representing the data type of the expression.

typeof[()expression[]] ;

The expression parameter is an arbitrary expression that needs to find type information.

instructions

The typeof operator returns the type information as a string. There are six possible return values for typeof: "number," "string," "boolean," "object," "function," and "undefined."
Parentheses in the typeof syntax are optional. Introduction to typeof:
typeof is a 1-meta operation, placed before an operand, which can be of any type.
It returns a value of 1 string that specifies the type of operand.

Do you know the result of typeof?

typeof(1);
typeof(NaN);
typeof(Number.MIN_VALUE);
typeof(Infinity);
typeof("123");
typeof(true);
typeof(window);
typeof(document);
typeof(null);
typeof(eval);
typeof(Date);
typeof(sss);
typeof(undefined);

How many do you know?

If you don't quite understand it after reading it, please look below (those who do not need to read it) :
typeof is a 1-tuple operator that always returns a string and returns different results for different operands.

The specific rules are as follows:

1. For numeric type operands, typeof returns the value number. For example, typeof(1) returns number.

The normal number cited above returns number for the unconventional number type. For example, typeof, NaN
JavaScript represents a special non-numeric value, although it is itself a numeric type.

In JavaScript, there are several other specific number types:

Infinity stands for infinite special value
NaN special non-numeric value
Number.MAX_VALUE maximum number that can be represented
Number.MIN_VALUE the smallest number that can be represented (closest to zero)
Number.NaN special non-numeric values
Number.POSITIVE_INFINITY represents a special value of positive infinity
Number.NEGATIVE_INFINITY represents a special value for negative infinity

The above special type, in the operation with typeof, the result will be number.

2. For the string type, typeof returns string. For example, typeof("123") returns string.
3. For Boolean types, typeof returns the value boolean. For example, typeof(true) returns boolean.
4. The value returned for object, array, null is object. For example, typeof(window), typeof(document), and typeof(null) all return object.
5. For function types, the return value is function. For example: typeof(eval), typeof(Date) return function.
6. Returns undefined if the operand is undefined (such as a nonexistent variable, function, or undefined). For example: typeof(sss), typeof(undefined) return undefined.

This is the end of this article, I hope you enjoy it.


Related articles: