【JavaScript---自学复习用】二、if-else、类型转换、真值假值、双等号与三等号

1、if-else (很简单不解释了哈

//if - else
const age = 19;
const isOldEnough = age >= 18;

if (age >= 18) {
    //ctrl +i 调用出emjoy表情
    console.log('Sarah can start driving licnese 🚗')
} else {
    const yearsLeft = 18 - age;
    console.log("Sarah is too young.Wait anoher ${yearsLeft} years :)")
}

const birthYear = 1991;
if (birthYear <= 2000) {
    century = 20;
} else {
    century = 21;
}

console.log(century)

2、类型转换

//类型转换
const inputYear = "1991";
//转换成数字
console.log(Number(inputYear), inputYear); //1991 '1991'
console.log(Number(inputYear) + 18); //2009
//转换成字符串
console.log(String(23), 23); //23 23

//强制类型转换
console.log("I am " + 23 + "years old"); //I am 23years old
//加号不行
console.log("23" + "10" + 1); //23101
console.log("23" - "10" - 3); //10
console.log("23" / "2"); //11.5

let n = "1" + 1;
n = n - 1;
console.log(n); //10

3、真值假值

//真值 假值
//5个假值 0, ' ', undefined , null , NaN
//真值 任何非零数字或者任何非零字符
console.log(Boolean(0));//假
console.log(Boolean(undefined));//假
console.log(Boolean('Jonas'));//真
console.log(Boolean({}));//真
console.log(Boolean(''));//假

//类型强制 布尔值 检查变量是否被定义
//1.使用逻辑运算符
//2.使用if-else
const money = 100;
if (money) {
    console.log("Don't spend it at all")
} else {
    console.log("You should get a job");
}

let height;
if (height) {
    console.log('Height is defined');
} else {
    console.log('Height is UNDEFINED');
}

4、双等号与三等号

/
// == 判断值
// === 判断值 也判断类型 是否完全相同
const age = 18;
if (age === 18) console.log('You just become an adult ');

console.log('18' == 18);//true
console.log('18' === 18);//false

const favourite = prompt("What is your favourite number?");
console.log(favourite);
console.log(typeof favourite)

if (favourite === 23) {
    console.log('Cool===!')
} else if (favourite === 7) {
    console.log(777777777);
} else {
    console.log('not 23 or 7');
}

if (favourite !== 23) console.log('why not 23');
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值