JavaScript checks the type of the variable and determines whether it is an integer, a string, or another type.
<Html> <pead> <title> check the variable type </title> </pead> <body> </ptml>
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
2. toString is originally used for String Conversion, but it is now popular for checking the variable type. Shun zi also wrote a function to check the type of the variable. It can be used to replace typeof
Copy codeThe Code is as follows:
Function getType (o ){
Var _ t; return (_ t = typeof (o) = "object "? O = null & "null" | Object. prototype. toString. call (o). slice (8,-1): _ t). toLowerCase ();
}
Execution result:
GetType ("abc"); // string
GetType (true); // boolean
GetType (123); // number
GetType ([]); // array
GetType ({}); // object
GetType (function () {}); // function
GetType (new Date); // date
GetType (new RegExp); // regexp
GetType (Math); // math
GetType (null); // null