
批改狀態(tài):合格
老師批語:
JavaScript 的數(shù)據(jù)類型,可以大致分為以下幾種:
數(shù)值,字符串,布爾值,undefined,null都是原始類型,即它們是最基本的數(shù)據(jù)類型,不能再細(xì)分了,它們有以下兩個特點:
let a = 1;
let a = 1;
console.log(typeof a);
a = "str";
console.log(typeof a);
函數(shù),數(shù)組,對象,因為一個它們往往是多個原始類型的值的合成,可以看作是一個存放各種值的容器
const myArrey = ["我會在哪里呢", "你能猜到嗎", true];
console.log(myArrey);
// 單獨顯示數(shù)組里的元素要使用索引/下標(biāo)
console.log(myArrey[0]);
const myCar = { name: "benz", model: "E300L" };
console.log(myCar);
console.log(myCar["name"]);
console.log(myCar.model);
如果變量的值是原始數(shù)據(jù)let a = 1;
,那么在a內(nèi)存中保存的就是變量名a和數(shù)據(jù)1
如果變量的值是引用類型函數(shù),數(shù)組或?qū)ο?code>const userInfo = {name:"Pharaoh", age:"30", skill:"吹牛"},那么userInfo內(nèi)存中保存的是對象的地址值,地址指向了內(nèi)存中保存{name:"Pharaoh", age:"30", skill:"吹牛"}
的位置:
使用if和switch,都可以完成條件判斷,只有滿足預(yù)設(shè)的條件,才會執(zhí)行相應(yīng)的語句
// 單分支
if (邏輯判斷)
操作;
// 雙分支
if (邏輯判斷) {
滿足條件時,執(zhí)行的操作;
}
else {
不滿足條件時,執(zhí)行的操作;
}
// 多分支
if (邏輯判斷) {
滿足條件時,執(zhí)行的操作;
}
else if (邏輯判斷){
滿足條件時,執(zhí)行的操作;
}
else if (邏輯判斷){
滿足條件時,執(zhí)行的操作;
}
else {
不滿足條件時,執(zhí)行的操作;
}
實例
// 單分支
let x = 1;
if (x === 1) {
console.log("X等于:" + x);
}
// 雙分支
if (x === 2) {
console.log("X等于:" + x);
} else {
console.log("X不等于:" + 2);
}
// 多分支
let car = "凱迪拉克";
if (car === "凱迪拉克") {
console.log(car + "不拉客,只拉美女和模特");
} else if (car === "奧迪") {
console.log("EA888誰修誰發(fā)家");
} else if (car === "大眾") {
console.log("EA888誰修誰發(fā)家");
} else {
console.log("抱歉本會所暫不接待您的品牌");
}
多個if else if一起使用的的時候可以使用更方便的switch
switch (true) {
case 邏輯判斷:
滿足條件時,執(zhí)行的操作;
break;
case 邏輯判斷:
滿足條件時,執(zhí)行的操作;
break;
default:
以上邏輯都不滿足條件時,執(zhí)行的操作;
}
實例
let myLuckyNum = 8;
switch (true) {
case myLuckyNum === 8:
console.log("恭喜你答對了");
break;
case myLuckyNum > 8:
console.log("太大了");
break;
case myLuckyNum < 8:
console.log("太小了");
break;
default:
console.log("我的意大利炮呢?");
}
// 單值
myLuckyNum = "8";
switch (myLuckyNum) {
case 8:
console.log("恭喜你答對了");
break;
default:
console.log("別亂輸入");
}
條件判斷拓展:三元運算符
三元運算符可以理解為if…else的簡寫形式(條件) ? 表達(dá)式1 : 表達(dá)式2
let x = (x === n) ? "X和N相等" : "X和N不相等"
如果“條件”為true,返回“表達(dá)式1”的值,否則返回“表達(dá)式2”的值
break和continue
break語句和continue語句都具有跳轉(zhuǎn)作用,可以讓代碼不按既有的順序執(zhí)行
let i = 0;
while (i < 100){
i++;
if (i === 5)
break;
console.log('i 當(dāng)前為:' + i);
}
let i = 0;
while (i < 100){
i++;
if (i === 6)
continue;
console.log('i 當(dāng)前為:' + i);
}
循環(huán)可以重復(fù)執(zhí)行操作,有while和for兩種
while循環(huán)需要一個條件,如果條件為真(true),就會重復(fù)執(zhí)行代碼區(qū)塊里的操作
while (條件) {
操作;
}
實例
// while
let userNum = 1;
while (userNum <= 8) {
console.log(userNum);
userNum++;
}
do while循環(huán)與while循環(huán)類似,唯一的區(qū)別就是先運行一次循環(huán)體,然后判斷循環(huán)條件
do {
操作;
} while (條件);
實例
// do while
userNum = 0;
do {
userNum++;
console.log("當(dāng)前用戶為: " + userNum);
} while (userNum < 3);
for循環(huán)需要指定循環(huán)的起點(計數(shù)器),條件和遞增操作
for (起點(計數(shù)器); 條件; 遞增操作) {
操作;
}
實例
// for
let wishList = [
{ hat: "northface", color: "black" },
{ shose: "dunk", color: "black" },
{ shirt: "jack", color: "black" },
];
let wishNum = wishList.length;
for (i = 0; i < wishNum; i++) {
console.log(wishList[i]);
}
for in遍歷的是對象的屬性的索引/下標(biāo)
let wishList = [
{ hat: "northface", color: "black" },
{ shose: "dunk", color: "black" },
{ shirt: "jack", color: "black" },
];
for (let n in wishList) {
console.log(n);
}
// 可以使用數(shù)組的索引得到元素
for (let n in wishList) {
console.log(wishList[n]);
for of遍歷的是對象的屬性所對應(yīng)的元素
for (let n of wishList) {
console.log(n);
}
// 可以用內(nèi)置的方法獲得鍵(key)和索引
for (let n of wishList.keys()) {
console.log(n);
}
// 可以用內(nèi)置的方法獲得鍵(key)和值(values)
for (let n of wishList.entries()) {
console.log(n);
}
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號