Home > Web Front-end > JS Tutorial > Js code to check variable type ()_javascript skills

Js code to check variable type ()_javascript skills

WBOY
Release: 2016-05-16 18:23:55
Original
1068 people have browsed it

JavaScript checks the type of the variable and determines whether it is an integer or a string or other types, etc.


[Ctrl A Select all Note: If you need to introduce external Js, you need to refresh to execute
]

2. toString is originally used It is used for string conversion, but now it is popular for checking variable types. Shunzi also wrote a function here to facilitate checking the type of variables, which can be used instead of typeof The 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
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template