Javascript basic tutorial typeof operator
typeof operator
typeof is used to detect the data type of variables. The following characters will be returned for values and variables
Undefined
In JavaScript, undefined is a variable that has no set value.
typeof A variable with no value will return undefined.
Let’s write an example below:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>检测数据类型</title> <script type="text/javascript"> var box; document.write(typeof box); </script> </head> <body> </body> </html>
The above code will output undiffed
Boolean type
The following example:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>检测数据类型</title> <script type="text/javascript"> var box= true; document.write(typeof box); </script> </head> <body> </body> </html>
string type
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>检测数据类型</title> <script type="text/javascript"> var box="php 中文网"; document.write(typeof box); </script> </head> <body> </body> </html>
number type
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>检测数据类型</title> <script type="text/javascript"> var box=888; document.write(typeof box); </script> </head> <body> </body> </html>
Object
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>检测数据类型</title> <script type="text/javascript"> var box={}; document.write(typeof box); </script> </head> <body> </body> </html>
Note: An empty object means that the object has been created, but there is no content in it
NULL empty
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>检测数据类型</title> <script type="text/javascript"> var box=null; document.write(typeof box); </script> </head> <body> </body> </html>
function function
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>检测数据类型</title> <script type="text/javascript"> function box(){ } document.write(typeof box); </script> </head> <body> </body> </html>
Note: box is a function function. The return value is function box(){}. The returned character channeling type is function