1、js定义变量:
x=10;
2、js输出变量:
alert(x);
document.write(x);
alert(x);
<script type="text/javascript">
x=10;
alert(x);
</script>
document.write(x)
<script type="text/javascript">
x=10;
document.write('<h1>aaaaaaaaaaaaaaaaaaaaaa</h1>')
</script>
3、字符串连接符:
x+y;
<script type="text/javascript">
y='hello ';
x='word';
alert(y+x);
</script>
4、js注释:
1.单行注释
//
2.多行注释
/* */
5、js的10种变量类型:
A:标准类型
1.整型
x=10;
2.浮点型
x=10.5;
3.字符串
x=‘hello world!’;
4.布尔型
1)真(true) x=‘true’
2)假(false) x=‘false’
6、B:复合类型
5.数组
1)数组定义
a.x=new Array(1,2,3);
b.x=[1,2,3];
x=new Array(1,2,3,'user',new Array(1,2,3,'user',));
alert(x);
x=[1,2,3,'user',[1,2,3,'user']]
2)数组类型
a.一维数组
arr=[1,2,3];
b.二维数组
arr=[1,[2]];
c.三维数组
arr=[1,[2,[3]]];
3)数组元素
arr=[‘a’,‘b’];
a.第一个元素:0=>‘a’
b.第二个元素:1=>‘b’
数组的键
x=[1,2,3,'user',[1,2,3,'user',[1,2,3,'user']]]
alert(x[4][4][3])
6.对象
obj=new Object();
obj.username=‘user123’;
obj.age=20;
obj.say=function(){
alert('my name is '+this.username);
}
//js对象
obj=new Object();
alert(obj);
对象组成部分:
1.属性(特征)
2.方法(行为)
7.json对象
obj={
'username':'user1',
'age':'20',
'sex':'nan'
};
``````
# 7、C:特殊类型
8.NaN
str='10abc';
num=Number(str); #就是NaN类型
9.null
str=null;
10.undefined
alert(str);
8、浏览器F12控制台调试
x=[1,2,3,'user',[1,2,3,'user']]
y='欢迎访问xxxxx'
console.log(x+y);
9、类型测试
1)typeof();
2)x.constructor;
if(x.constructor==Array){
alert(1);
}
3)x instanceof Array
if(x instanceof Array){
alert(1);
}
constructor检测数组类型
//js中检测数组类型
x=[];
if(x.constructor==Array){
alert('array');
}else{
alert('no array');
}
10、