JavaScript basics
JavaScript typeof usage
JavaScript typeof is a unary operator used to check what type of data is. After checking, it will return a string. For different data types, there will be different return strings, such as string, boolean, function, and object. ..... You can check it out, but you may not find the result if you use it incorrectly.
JavaScript typeof syntax example
typeof (expression of the type to be checked);
In the example, the "expression of the type to be checked" may have many types. In the first paragraph, we mentioned that typeof() will return different results for different types of expressions, so we also prepared many of the following Example reference, examples are very simple values, it is easy to see the difference of each type.
JavaScript typeof implementation example
<script language="javascript">
document.write(typeof(123)+'<br>'); // output number
document.write(typeof(NaN)+'<br>'); // output number
document. write(typeof('123')+'<br>'); // output string
document.write(typeof(true)+'<br>'); // output boolean
document.write(typeof(window)+' <br>'); // Output object
document.write(typeof(null)+'<br>'); // Output object
document.write(typeof(Date)+'<br>'); // Output function
document.write(typeof(undefined)+'<br>'); // output undefined
</script>
The first line of the example tests the number string 123, and the number is output smoothly, while the third line is the same test 123, but with single quotes, JavaScript will treat it as a string, so it outputs string. The NaN in the second line is JavaScript represents a type of number, so it also outputs number. Then, true and false belong to the boolean value. The window and null in the next two lines belong to the object, and Date is a date function, so the output function.document.write(typeof(123)+'<br>'); // output number
document.write(typeof(NaN)+'<br>'); // output number
document. write(typeof('123')+'<br>'); // output string
document.write(typeof(true)+'<br>'); // output boolean
document.write(typeof(window)+' <br>'); // Output object
document.write(typeof(null)+'<br>'); // Output object
document.write(typeof(Date)+'<br>'); // Output function
document.write(typeof(undefined)+'<br>'); // output undefined
</script>
Post a Comment
0 Comments