javascript数据类型包含以下几种:
字符串(String)、数字(Number)、布尔(Boolean)、数组(Array)、对象(Object)、空(Null)、未定义(Undefined),javascript拥有动态的数据类型,即使用同一个变量能够作用不同的数据类型。
例如:
var x;//变量x的类型为undefined,是一个只声明但是没有定义的变量
var x=5;//此时x变量的数据类型为数字(Number),值为5
var x="name";//此时x变量的数据类型是字符串(String),值为name
JavaScript字符串
用于存储字符类型的值。可以将值至于单引号或者双引号中。
注:可以在字符串中使用引号,但是要注意的是,字符串中使用的引号一定不可以和包围字符串的引号匹配。
例如:
<!DOCTYPE html>
<html>
<body>
<script>
var phone1="Iphone7";
var phone2='Iphone7s';
var text1="这是一段话'明白'?";
var text2='这是另一段话"明白"?';
document.write(phone1+"<br/>");
document.write(phone2+"<br/>");
document.write(text1+"<br/>");
document.write(text2+"<br/>");
</script>
</body>
</html>
结果:
JavaScript数字
在JavaScript中只存在一种数字类型。在给变量赋值(数字)的时候,赋值的数字可以是带小数点的也可以是不带小数点的。
另外,我们还可以使用科学计数法来书写一些极大或极小的数字。
案例:
<!DOCTYPE html>
<html>
<body>
<script>
var x=12.00;
var y=12;
var z=1e8;//100000000
var j=45e-5;//0.00045
document.write("x="+x+"<br/>");
document.write("y="+y+"<br/>");
document.write("z="+z+"<br/>");
document.write("j="+j+"<br/>");
</script>
</body>
</html>
结果:
JavaSript布尔类型
布尔类型(逻辑)只能有两个值:true或false。
var x=true;
var y=false;
注意咯,这个和字符串是不一样的。
JavaScript数组
创建数组的有3种方式:
第一种:先创建一个空数组,再向数组中添加元素。
var phone1=new Array();//创建一个空数组
phone1[0]="诺基亚";//向phone数组下标为0的位置(即数组中的第一个元素)传入值为“诺基亚”
phone1[1]="三星";//向phone数组下标为1的位置传值(即数组中的第二个元素)传入值为“三星”
phone1[2]="苹果";//向phone数组下标为2的位置传值(即数组中的第三个元素)传入值为“苹果”
第二种:condensed array
var phone2=new Array("摩托罗拉","华为","小米");
第三种:literal array
var phone3=["金立","锤子手机","步步高"];
注意:数组下标是基于零的,所以第一个元素是[0],第二个元素是[1],以此类推。
案例:
<!DOCTYPE html>
<html>
<body>
<script>
//第1种
var phone1=new Array();
phone1[0]="诺基亚";
phone1[1]="三星";
phone1[2]="苹果";
document.write("第1个数组phone1中的元素有:"+"<br/>");
document.write(phone1[0]+"<br/>");
document.write(phone1[1]+"<br/>");
document.write(phone1[2]+"<br/>");
var phone2=new Array("摩托罗拉","华为","小米");
document.write("第2个数组phone2中的元素有:"+"<br/>");
document.write(phone2[0]+"<br/>");
document.write(phone2[1]+"<br/>");
document.write(phone2[2]+"<br/>");
//第3种
var phone3=["金立","锤子手机","步步高"];
document.write("第3个数组phone3中的元素有:"+"<br/>");
document.write(phone3[0]+"<br/>");
document.write(phone3[1]+"<br/>");
document.write(phone3[2]+"<br/>");
//还可以通过循环遍历数组的方式输出document.write("通过循环遍历的方式输出的phone1的元素:"+"<br/>");
var i;
for(i=0;i<phone1.length;i++)
{
document.write(phone1[i]+"<br/>");
}
</script>
</body>
</html>
结果:
JavaScript对象
对象由花括号分隔。在括号的内部,对象的属性以名称和值对的形式(name:value)来定义。属性由逗号分隔:
var student={name:"张三",address:"中国",id:13540213};//student对象中有3个属性:name,address,id
值得注意的是,数组的分隔用[]而对象的用{}
另外,使用空格个折行不影响对象属性,有的时候使用空格和折行可以使得编码看起来更整洁
var student={
name:"张三",
adress:"中国",
id=13540213
};
对象有两种寻址方式
document.write(person.lastname + "<br>");
document.write(person["lastname"] + "<br>");
下面给出完整代码:
<!DOCTYOE html>
<html>
<body>
<script>
var student={
name:"张三",
adress:"中国",
id:13540213
};
document.write("学生姓名:"+student.name+" 学生地址:"+student.adress+" 学生ID:"+student.id+"<br/>");
document.write("学生姓名:"+student["name"]+" 学生地址:"+student["adress"]+" 学生ID:"+student["id"]);
</script>
</body>
</html>
结果: