js课程 1-5 js如何测试变量的数据类型

js课程 1-5 js如何测试变量的数据类型

一、总结

一句话总结:用typeof()方法。

1、js如何判断变量的数据类型?

用typeof()方法。

13 v=10;
14 
15 if(typeof(v)=='string'){
16     alert('字符串');

2、typeof()可判断的类型有哪五类?

1.string
2.number
3.boolean
4.obejct
5.undefined

15 if(typeof(v)=='string'){

3、js定义数组的两种方法?

array对象和[]

13 // arr=new Array(1,2,3);
14 arr=[1,2,3];

4、js中如何测试数组类型?

用arr instanceof Array

16 alert(arr instanceof Array);

5、js中哪两种对象的方法可以省略对象名?

Global对象和window对象

1.Global对象(js内部对象)
• typeof();
• parseInt();
• parseFloat();
• eval();
• Number();
• String();
• Boolean();

2.window对象(浏览器提供对象)
• alert();

6、js中global对象的常用方法有哪些?

Global对象(js内部对象)
• typeof();
• parseInt();
• parseFloat();
• eval();
• Number();
• String();
• Boolean();

二、js如何测试变量的数据类型

1、相关知识

变量类型测试:
1.typeof();
2.arr instanceof Array;

typeof()可判断的类型:
1.string
2.number
3.boolean
4.obejct
5.undefined
#一定要注意js中所有变量都是对象

arr instanceof Array可以测试数组类型:

js中前面的对象不用写的两种情况:
1.Global对象(js内部对象)
• typeof();
• parseInt();
• parseFloat();
• eval();
• Number();
• String();
• Boolean();

2.window对象(浏览器提供对象)
• alert();

2、代码

typeof变量类型判断

 1 <!doctype html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>index</title>
 6 </head>
 7 <body>
 8     <h1>aaaaaaaaaaaaaaaaaaaaaaaaaaa</h1>    
 9 </body>
10 <script>
11 //类型判断
12 
13 v=10;
14 
15 if(typeof(v)=='string'){
16     alert('字符串');
17 }else{
18     alert('不是字符串');
19 }
20 </script>
21 </html>

instanceof判断数组类型

 1 <!doctype html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>index</title>
 6 </head>
 7 <body>
 8     <h1>aaaaaaaaaaaaaaaaaaaaaaaaaaa</h1>    
 9 </body>
10 <script>
11 //判断数组类型
12 
13 // arr=new Array(1,2,3);
14 arr=[1,2,3];
15 
16 alert(arr instanceof Array);
17 </script>
18 </html>
 
原文地址:https://www.cnblogs.com/Renyi-Fan/p/9233433.html