//单行注释
/*多行注释*/
var x=true; //Boolean变量
var date=314; //声明一个变量 变量名称区分大小写
var name='Songer';
var introduce="I am Songer";//两种声明字符串都可以
/*
\ 转义字符
\' single quote 单引号
\" double quote 双引号
\\ backslash 反斜杠
\n new line 新一行
\r carriage return 回车
\t tab
\b backspace 空格
\f form feed 换页
x+=y x=x+y
x*=y x=x*y
x-=y x=x-y
x/=y x=x/y
x%=y x=x%y
== Equal to 等于
=== Identical(equal and of same type) 等于且类型相同
!= Not equal to 不等于
!== Not Identical 类型不同
> Greater than 大于
>= Greater than or equal to 大于等于
< Less than 小于
<= Less than or equal to 小于等于
&& Returns true if both operands are true 如果为true 全都是true
|| Returns true if one of the operands is true 如果为true 有一个true
! Returns true if the operands is false,and false,if the operands is true 如果为true 则为false
三元运算式
variable = (condition) ? value1: value2 变量 = (条件)? 值1:值2
字符串+字符串 (+连接作用)
*/
//条件语句
if (condition) {
statements //条件为true执行
}else if(condition){
executed if condition is true
}
else{
executed if condition is false //条件为false执行
}
switch (expression) {
case n1:
statements //捕捉到执行
break;
case n2:
statements
break;
default:
statements //当没有找到匹配时执行
break;
}
//loops 循环
for (i=1; i<=5; i++) {
//code block
}
while (condition) {
//code block
}
//先执行一次do
do {
//code block
}
while (condition);
//声明一个name方法
function name(){
//code to be executed
}
//声明一个Name方法 但有参数
function Name(param1, param2, param3) {
// some code
}
name();//执行这个方法
break //中断关键字
continue //跳出此次循环
return //返回
document.write(x);//在页面上打印x值
alert("这是一个警告框");
var user=prompt("这是一个提示框");
alert(user);
var result = confirm("这是一个确认框");
if (result == true) {
alert("你点击了确认");
}
else {
alert("你点击了取消");
}
//声明对象
var person = {
name: "Songer", age: 20, height: 175
};
//使用属性
var x = person.age;
var y = person['age'];
//长度(系统内置属性)
x.length
function person(name,age){
this.name=name;
this.age=age;
this.changeName=function(name){
this.name=name;
}
}
var p=new person("Songer",20);//创建对象
p.changeName("怂儿"); //修改名称
var courses = new Array("HTML", "CSS", "JS"); //声明数组
var course= courses[0]; //HTML 获取 获取不到返回undefined
courses[1]="C++"; //修改
var c1 = ["HTML", "CSS"];
var c2 = ["JS", "C++"];
var courses = c1.concat(c2); //c1.concat(c2);组合在一起
var person = []; //empty array 空数组
person["name"] = "Songer";
person["age"] = 20;
document.write(person["age"]);
function myAlert() {
alert("Hi");
}
setInterval(myAlert,3000)//每三秒执行一次方法
clearInterval(myAlert);//停止方法