1.条件判断
var age=10; if(age<12){ console.log("儿童"); }else if(age>=12&& age<30 ){ console.log("青年"); }else{ console.log("老年"); }
if (age<12) { console.log('asdfghj'); }else{ console.log(23456789); }
var tag=age<12?"儿童":"非儿童" //三目运算符
var condition=2; switch(condition){ case 4: alert("c的值是4"); break; case 3: alert("c的值是3"); break; case 2: alert("c的值是2"); break; case 1: alert("c的值是1"); break; default: alert ("other"); }
if (condition == 3 || condition == 2 || condition == 1)
case里面的条件是否符合switch括号里的条件 age = 20; switch(true) { case age < 12: console.log('儿童') break case age >=12 && age < 30: console.log('青年') break case age >= 30: console.log('老年') break } 2.循环 for (var i=1; i<10; i++) { console.log(i) }
var j = 1
while(j<10) {
console.log(j)
j++
} <可能一次不执行>
var k=-1;
do{
console.log(k);
k--;
}while(k>0)<可能一次不执行>
while(true){ alert('hahh') break; }
3.函数 定义函数 function 方法名 function fun(){ console.log("haha"); } 函数的调用 fun();
var fn=function(){ console.log("12345"); } fn();
function add(x,y){ return x+y;(直接返回 return后面的语句不会执行) console.log("hehe") } add(1,2);
var aaa=add(2,3); console.log(aaa);
错误写法 var fn1; fn1(); //fn1 is noy a function var fn1=function(){ console.log(12) } fn1();
4.变量的提升 只有通过var定义的变量才会出现变量提升 var a = 1; //全局变量 b = 2; //如果没有通过var定义的变量会当成全局变量 函数体内部定义的变量是局部变量,全局作用域里无法调用局部变量 局部作用域里可以调用全局变量
function fun() {
var i = 1
console.log(b) // 2
b = 5;
console.log(b) // 5
}
console.log(i) //i is not defined
fun()
console.log(b) // 5
5.数组 var arr=[]; console.log(arr); <Array(0)>
var arr1=new Array() <定义空数组 等同于 var arr=[];> console.log(arr1); <Array(0)>
var arr2=[1,2,3]; console.log(arr2.length); <3> console.log(arr2[2]); <3> console.log(arr2[3]);
var arr3=new Array(1,2,4); console.log(arr3); < 0:1 1:2 2:4 length:3 >
var arr4=new Array(4); <数组长度为4的空数组> arr4[0]=123; console.log(arr4); < Array(4) 0:123 length:4
new Array() 定义数组 如果传多个值 就是数组的初始化赋值 如果只传一个值的话代表定了数组的长度 但是是空数组
var arr5 = [1,2,3,4,5,6,7]; for(var i=0; i<arr5.length ;i++) { console.log(arr5[i]); } <1 2 3 4 5 6 7>
var arr6 = [1,2,'3','hello', true, undefined]; console.log(arr6); < 0:1 1:2 2:"3" 3:"hello" 4:true 5:undefined length:6>
concat 连接两个数组 返回一个新的数组 var a1=[1,2]; var a2=[2,34]; var a3=a1.concat(a2); console.log("a1:",a1); console.log("a2:",a2); console.log("a2:",a2); < 0:1 1:2> < 0:2 1:34> < 0:1 1:2 2:2 3:34>
push 向数组的末尾添加元素 会改变原数组 返回数组的长度 var len=a1.push(3,4); console.log(len); console.log(a1); < 4 > < 0:1 1:2 2:3 3:4>
pop 删除数组的最后一个元素 会改变原数组 var t=a1.pop(); console.log(t); console.log(a1); < 0:1 1:2 2:3> < 0:1 1:2 2:3>
unshift 向数组的开头添加元素 返回新数组的长度 原数组改变 var l2 = a1.unshift(11,89); console.log(l2); console.log(a1); [5] [11,89,1,2,3]
shift 删除第一个元素 返回被删除的元素 原数组会改变 var t2=a1.shift(); console.log(t2); console.log(a1); [89,1,2,3] [89,1,2,3]
slice(start,end) 从start开始截取到end(不包含end)返回一个新数组 原数组不变 如果只传一个值 就是一直截取到数组结尾 var tmp=a1.slice(1); console.log(tmp); console.log(a1); var a4=a1.slice(1,2); console.log(a4); [1,2,3] [89,1,2,3] [1]
splice(index,howmany,x1,x2...) 数据添加或删除元素 返回新数组 原数组会改变 var b=[1,2,3,4,5,6]; var b1=b.splice(1,3); console.log(b); // [1,5,6] console.log(b1); // [2,3,4]
splice (index,howmany,x1,x2) 删除第index元素 追加到howmany个元素 分别是x1,x2 var b3 =[1,2,3]; var b2=b3.splice(2,3,1,2,3) console.log(b2); console.log(b3); [] [1 2 1 2 3]
reverse() 数组倒序该方法会改变原来的数组,而不会创建新的数组。 var b4 = [2,1,4,5]; var b5 = b4.reverse(); console.log(b4); console.log(b5); [5,4,1,2] [5,4,1,2]
sort() 对数组排序 会改变原数组 sort() 如果不传值 会按照字典序进行排列 function s1(a,b) { // return a-b; //升序排列 return b-a; //降序排列 }
var b6 = [31,12,1,26,9,0, 2,37];
// var b7 = b6.sort(s1);
// console.log(b6);
// console.log(b7);
var b8 = b6.sort();
console.log(b8);
toString 将数组转换成字符串 中间用,分割 var c1 = ['hello', 'world']; var c2 = c1.toString(); console.log(c1); console.log(c2); [0:"hello" 1:"world"] [hello,world]
join 将数组转换称字符串 可以用指定字符分割 var str = c1.join(';'); console.log(str); [hello;world]
6.对象 // 获取对象下属性的方法 var tmp = 'name'; console.log(p1.name); console.log(p1['name']); console.log(p1[tmp]);
-
基本数据类型的赋值 就是把值进行赋值 存在栈里 引用数据类型的赋值 是把地址进行赋值 如果修改其中一个的内容另一个也会跟着修改 地址存在栈里 值存在堆里
-
var myDate = new Date()
console.log(myDate.getDate()); console.log(myDate.getDay()); console.log(myDate.getMonth()+1); console.log(myDate.getFullYear()); console.log(myDate.getTime());
var date = new Date(); var year = date.getFullYear(); var month = date.getMonth(); var day = date.getDate();
var str = year + '年' + (month + 1) + '月' + day + '日'; console.log(str);
9.字符串对象 var str = 'hello world';
console.log(str.length);
var s1 = new String();
console.log(s1);
// replace 字符串的替换
var s2 = str.replace('world', 'xiaoming');
console.log(s2);
// split 把字符串转成数组
var a1 = str.split('&');
console.log(a1);
// indexOf 检索字符串中某字符首次出现的位置,如果没找没到返回-1
console.log(str.indexOf('a'));
// lastIndexOf 检索字符串中某字符最后一次出现的位置
console.log(str.lastIndexOf('l'));
console.log(str.toUpperCase().toLowerCase());
// substr(index, length) 从下标未index开始,截取length长度的字符
// hello world
console.log(str.substr(1,2));
// substring(start, stop) 截取start和stop之间的字符 不包括stop
console.log(str.substring(1,2));
// slice(start,end) 截取start和end之间的字符 不包括end , 能使用负数
console.log(str.slice(-3,-2));