I recently made a project that involves processing JS to determine data types, searched for relevant information online, and personally tested the judgment of various data types, which is absolutely safe. Below, the editor will share the specific content with you for your reference!
1. Data types in JS
1. Numerical type (Number): includes integers and floating point numbers.
2. Boolean
3. String type
4. Object
5.Array
6.Null value (Null)
7. Undefined
2. Determine the data type of a variable
1.Number type (number)
The most commonly used judgment methods are:
function isNumber(val){return typeof val === 'number';}But some situations don't work, such as:
var a;alert(isNumber(parseInt(a)));
What pops up here is true, as shown in the figure below:
But in fact, the variable a is NaN, which cannot be used for numerical operations.
So the above function can be modified to:
function isNumber(val){return typeof val === 'number' && isFinite(val);}After modification, false pops up, as shown in the figure below:
By the way, the JavaScript isFinite() function is used to check whether its parameters are infinite. If number is a finite number (or can be converted to a finite number), then returns true. Otherwise, if number is NaN (non-number), or a positive or negative infinity number, false is returned.
2. Boolean
The Boolean type judgment is relatively simple, and you can use the following method to make judgments:
/*Judge whether the variable val is a boolean type*/function isBooleanType(val) {return typeof val ==="boolean";}Test code:
<script type="text/javascript">/*Judge whether the variable val is a boolean type*/function isBooleanType(val) {return typeof val ==="boolean";}var a;var b = false;alert("The result of the variable a is a boolean type is: "+isBooleanType(a));alert("The result of the variable b is a boolean type is: "+isBooleanType(b));</script>Running results:
3. String
The judgment of string type is relatively simple, and you can use the following method to make judgments:
/*Judge whether the variable is a string type*/function isStringType(val) {return typeof val === "string";}Test code:
<script type="text/javascript">/*Judge whether the variable is a string type*/function isStringType(val) {return typeof val === "string";}var a;var s = "strType";alert("The result of the variable a is a string type is: "+isStringType(a));alert("The result of the variable s is a string type is: "+isStringType(s));</script>Running results:
4. Undefined
Undefined judgments are relatively simple, and can be judged by the following method:
/*Judge whether the variable is Undefined*/function isUndefined(val) {return typeof val === "undefined";}Test code:
<script type="text/javascript">var a;//a is undefinedvar s = "strType";/*Judge whether the variable is Undefined*/function isUndefined(val) {return typeof val === "undefined";}alert("The result of the variable a is Undefined is: "+isUndefined(a));alert("The result of the variable s is Undefined is: "+isUndefined(s));</script>Running results:
5. Object
Since when the variable is null, typeof will also return object, the Object cannot be judged directly with typeof.
It should be like this:
function isObj(str){if(str === null || typeof str === 'undefined'){return false;}return typeof str === 'object';}Test code:
<script type="text/javascript">/*Judge whether the variable is Object type*/function isObj(str){if(str === null || typeof str === 'undefined'){return false;}return typeof str === 'object';}var a;var b = null;var c = "str";var d = {};var e = new Object();alert("b's value is null, typeof b ==='object''' judgment result is: "+(typeof b ==='object'));alert("The judgment result of variable a is Object type: "+isObj(a));//falsealert("The judgment result of variable b is Object type: "+isObj(b));//falsealert("The judgment result of variable c is Object type: "+isObj(c));//falsealert("The judgment result of variable d is Object type: "+isObj(d));//truealert("The judgment result of variable e is Object type: "+isObj(e));//true</script>Running results:
6.Null value (Null)
Just use val === null to determine the null value
function isNull(val){return val === null;}Test code:
/*Judge whether the variable is null*/function isNull(val){return val === null;}/*Test variable*/var a;var b = null;var c = "str";//Pop up the run result alert("The judgment result of variable a is null is: "+isNull(a));//falsealert("The judgment result of variable b is null type is: "+isNull(b));//truealert("The judgment result of variable c is null type is: "+isNull(c));//falseRunning results:
7.Array
The array type cannot be judged by typeof. Because when the variable is an array type, typeof will return object.
Here are two ways to determine the array type:
/*Judge whether the variable arr is an array method 1*/function isArray(arr) {return Object.prototype.toString.apply(arr) === '[object Array]';}/*Judge whether the variable arr is an array method 2*/function isArray(arr) {if(arr === null || typeof arr === 'undefined'){return false;}return arr.constructor === Array;}Test code:
<script type="text/javascript">/*Judge whether the variable arr is an array method 1*/function isArray(arr) {return Object.prototype.toString.apply(arr) === '[object Array]';}/*Judge whether the variable arr is an array method 2*/function isArray(arr) {if(arr === null || typeof arr === 'undefined'){return false;}return arr.constructor === Array;}//Test variable var a = null;var b = "";var c ;var arr = [,,];var arr = new Array();//Print the test result document.write("arr variable is an array type, typeof arr === The result of 'object' is: "+(typeof arr === 'object'));document.write("<br/>");document.write("---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- t.write("<br/>");document.write("The result of judgment that variable a is an array type is: "+isArray(a));document.write("<br/>");document.write("The result of judgment that variable b is an array type is: "+isArray(b));document.write("<br/>");document.write("Variable c is an array class The judgment result of type is: "+isArray(c));document.write("<br/>");document.write("The judgment result of variable arr being an array type is: "+isArray(arr));document.write("<br/>");document.write("The judgment result of variable arr being an array type is: "+isArray(arr));document.wr item("<br/>");document.write("--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ent.write("The judgment result of variable a is an array type is: "+isArray(a));document.write("<br/>");document.write("The judgment result of variable b is an array type is: "+isArray(b));document.write("<br/>");document.write("The judgment result of variable c is an array type is: "+isArray(c)); document.write("<br/>");document.write("The result of the judgment that the variable arr is an array type is: "+isArray(arr));document.write("<br/>");document.write("The result of the judgment that the variable arr is an array type is: "+isArray(arr));document.write("<br/>");</script>Running results:
The above content is the summary of JavaScript knowledge points (VI) of JavaScript judging variable data types. I hope it will be helpful to everyone. If you want to know more, please pay attention to the Wulin.com website!