批改状态:合格
老师批语:这是预热课作业,不需要写,不过多写也很好
// // 模板字符串let name = 'chw';name = "nan";// 方案一name = "bao\" bao \"";// 方案二name = 'bao"bao"';// 方案三name= `bao'bao'`;console.log(name)
// // 模板字符串let name = 'chw';let sex = "nan";console.log(name+sex)
// // 模板字符串let name = 'chw';let sex = "nan";console.log(`${name}sex`)模板字符串实例(字符串引号不要,连接符+不要,变量使用${变量})
let num = 30;let price = 100;// 字符串拼接的一般方式// let res ='商品数量'+num+',单价:'+price+'元,总计:'+num*price;// 模板字面量,使用插值进行简写,字符串引号不要,连接符+不要,变量使用${变量}let res =`商品数量${num},单价:${price}元,总计:${num*price}`;console.log(res)
let num = 10;let price = 99;// 字符串拼接与计算一般方式// let res ='商品数量'+num+',单价:'+price+'元,总计:'+num*price;// 模板字面量,使用插值进行简写,字符串引号不要,连接符+不要,变量使用${变量}let res =`商品数量${num},单价:${price}元,总计:${num*price}`;console.log(res)// strings代表字符串,var1表示变量1,var2表示变量2function show(strings,var1,var2){console.log(strings);console.log(var1,var2);}show`商品数量${num},单价:${price}元,总计:${num*price}`;
对象解构
const teacher = {name:"zhu",age:18};let name =teacher.name;let age =teacher.age;console.log(name,age)
const teacher = {name:"zhu",age:18};({name,age}= {name:"zhu",age:18});console.log(name,age)
数组解构
const teacher = ['name','age','sex']let [tc1,tc2,tc3]= teacherconsole.log(tc1,tc2,tc3)
1.单分支
1.1一般解构
let score = 70;if(score>=60){console.log('及格了');}1.2简写(大括号不写)
let score = 70;if(score>=60) console.log('及格了');2.双分支
let score = 50;if(score>=60) console.log('及格了');else console.log('补考吧,兄弟');2.1三元运算简化双分支
let score = 80;let str = (score>=60)?'及格':'补考';console.log(score)3.多分支
// &&且,||或let score = 120;if(score>=60&&score<80) console.log('及格了');else if (score>=81&&score<=100) console.log('优秀');// 成绩的有效区间else if (score>=0||score<=100) console.log('不存在');// 默认分支elseelse console.log('补考吧');4.switch
let score = 120;// 区间正确的switch (true) {// 条件一case (score >= 60 && score < 80): console.log('及格了'); break;// 条件2case (score >= 81 && score <= 100): console.log('优秀'); break;// 条件3case (score >= 0 || score <= 100): console.log('不存在'); break;// 输出default: console.log('补考吧');}
const arr =[1,2,3,4,5];console.log(arr);let i =0while(i<arr.length){console.log(arr[i]);i++}
数组遍历
1.for (初始化变量;设置循环条件;更新循环条件) {**}
const arr = [1,2,3,4,5]for (let i =0;i<arr.length;i++) console.log(arr[i])2.for与if
const arr = [1,2,3,4,5]for (let i =0;i<arr.length;i++) {if(i<3)console.log(arr[i])}3对象遍历
const tc={id:5,name:'zhu',qq:"8798",};for(let key in tc){console.log(tc[key])}4.数组遍历
for-in遍历同Python
arr =[123,"zhen",22]for(let x in arr){console.log(arr[x])}
5.foreach遍历数组是js常用
let age= [10,10,30,123];// age 里面传方法foreachage.forEach(function(value){console.log(value)})// 箭头函数去掉function变成=>与value的小括号age.forEach(value=>{console.log(value)})
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号