javascript属于弱类型,值包含:数字,字符串和布尔值,c++与java属于强类型;int a="a",string a="a" 报错;var a ;原始类型:数字number 字符串string 布尔值 Boolean undefind null,对象object,原始值组合一起;对象分为1.系统自带的对象,数组,函数2.自定义{} e.g三门课程=>60 61 62 ;var a=[60,61,62]下标 序号0开始,自增++,a[0]=60
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <meta name="viewport" content="width=device-width, initial-scale=1.0"> 6 <title>下标获取数值</title> 7 <script> 8 // var a=[60,61,62] 9 // undefined 10 // a[0] 11 // 60 12 // a[1] 13 // 61 14 // a[2] 15 // 62 16 // a[0]=90 17 // 90 18 // a[0] 19 // 90 20 </script> 21 </head> 22 <body> 23 </body> 24 </html>
数组:把原始类型按照顺序排列好,数组里面什么都可以有,原始类型
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <meta name="viewport" content="width=device-width, initial-scale=1.0"> 6 <title>数组,原始类型</title> 7 <script> 8 // var a [0,"string",false] 9 // Uncaught SyntaxError: Unexpected token [ 10 // var a=[0,"string",false] 11 // undefined 12 // a[0] 13 // 0 14 // a[1] 15 // "string" 16 // a[2] 17 // false 18 </script> 19 </head> 20 <body> 21 </body> 22 </html>
自定义对象var a1=60; var a2=61; var a3=62; var a={"a1":60,"a2":61,"a3":62}字符串代表序号var a={0:60,1:61,2:62}数组在这种自定义类型基础上做了排序,省略0
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <meta name="viewport" content="width=device-width, initial-scale=1.0"> 6 <title>自定义对象</title> 7 <script> 8 // var a1=60; 9 // var a2=61; 10 // var a3=62; 11 // var a={"a1":60,"a2":61,"a3":62} 12 // //字符串代表序号 13 // var a={"a1":60,"a2":61,"a3":62} 14 // undefined 15 // a["a1"] 16 // 60 17 // a["a2"] 18 // 61 19 // a["a3"] 20 // 62 21 22 var a={2:60,3:61,4:62} 23 var a={2:60,3:61,4:62} 24 undefined 25 a[2] 26 60 27 a[3] 28 61 29 </script> 30 </head> 31 <body> 32 </body> 33 </html>
var a;=>undefind var a=null; 空的object 这么多不同的数据类型,通过什么方法判别?typeof()帮助查看数据类型
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <meta name="viewport" content="width=device-width, initial-scale=1.0"> 6 <title>typeof()查看数据类型</title> 7 <script> 8 var a; 9 undefined 10 typeof(a) 11 "undefined" 12 a=1; 13 1 14 typeof(a) 15 "number" 16 a="a"; 17 "a" 18 typeof(a) 19 a=false 20 false 21 typeof(a) 22 "boolean" 23 a=[1]; 24 [1] 25 typeof(a) 26 "object" 27 a=null; 28 null 29 typeof(a) 30 "object" 31 var a="1111"; 32 undefined 33 typeof(a) 34 "string" 35 var b=Number(b) 36 undefined 37 typeof(b) 38 "number" 39 a=1 40 1 41 b=String(a) 42 "1" 43 typeof(a) 44 "number" 45 typeof(b) 46 "string" 47 </script> 48 </head> 49 <body> 50 </body> 51 </html>
数据类型转换?原始类型:数字number 字符串string 布尔值 Boolean "aaa"=>数字 number 显示转换 var a=7;var b=number(a);强式转换1+"1"=>"11"
原文地址:https://www.cnblogs.com/dhnblog/p/12364678.html
时间: 2024-10-10 23:31:23