【UdemyJS】Section2笔记(共24节)JS基础-Part1

【UdemyJS】Section2笔记(共24节)JS基础-Part1

Artificial-Fool Lv3

2-7 const and var

declare a variable: *let, const, var(out-dated-sh*t)* //actually not mandatory but you should; or else it will be on the global object.

recommend default: **const** ,then change to let when needed.

let:
    let age=30; / let  age;       //could be undifined
    age=31;

const:
    const birthYear = 1991;     //must be initialized
                                //and could not change

2-9 Operator Precedence

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_precedence

在同一行内:
Math Operator(先加减): Left to Right 👉//乘除优先级仍大于加减,详见优先级表;
then↓
Relational operators(后比较): Left to Right 👉
then↓
Assignment Operator(再赋值): Right to Left 👈

example: console.log(100-1 > 100-2) => true

2-10 Template Literals

利用``(反引号)代替 “” ‘’(引号),可编辑模板字符串/代替普通字符串
${variable}

反引号还可以省略换行符(\n\):
‘String with\n\multiple\n\lines’

String with multiple lines

2-14 Type Conversion and Coercion

类型转换:String(), Number(), Boolean();
类型强制修正:console.log( ‘I am ‘ + 23 + ‘yeas old.’ ) => ‘I am 23 years old.’
console.log( ‘23’ - ‘10’ - 3 ) => 10

  • 当字符串和数字“+”在一起时,Number强制转换为String;
  • 当字符串和数字“-”,“*”,“/”等在一起时,String强制转换为Number;
  • 当”+“”-“等混合运算时,按从左到右依次转化并运算

e.g. 个人认为在整十进位中会有大用处,类似人手写数字的逻辑。instead of 123*10+4, 现在可以 123+’4’.

2-15 falsy values and truthy values

  • falsy values会在Boolean()等转化后呈现False值,
    Five falsy values: 0, ‘’, undefined, null ,NaN(Not-a-Number)
    即数字变为0时,字符串为空时,变量等未定义时,返回值为null时,还有数值为字符串时等等。
  • 相反地,这五个值以外的为truthy values,呈True值.

2-16 ”Loose“ and “Strict” Equality Operators ( ”==“ and ”===“ )

  • “==”: does type coercion (18 == “18”)√
  • “===”: no type coercion (18 === 18)√
      • 之前学python时经常用“==,而在js中应以“===”平替。Javascript的双等号规则太过鬼畜,一般不建议使用。
        e.g. prompt(‘type in value’) 会在网页上弹出一个提示窗口(如Everything登录弹窗),可以输入信息并返回字符串。

2-17 Logical Operators

  • and(与): A && B
  • or(或): A || B
  • not(非): !A
    1
    2
    3
    4
    5
    6
    7
    8
    const hasDriversLicense=true;
    const hasGoodvision=true;
    const isTired=true;
    if(hasDriversLicense && hasGoodvision && !isTired ){
    console.log('Sarahisabletodrive!');
    }else{
    console.log('Someone elseshould drive...');
    }

2-20 The Switch Statement

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
const day = 'monday'
switch (day){
case 'monday':
console.log('Plan course structure');
console.log('Go to a coding meetup');
break;//阻止代码继续向下执行,跳出Switch语句
case 'tuesday':
console.log('Prepare theory videoos')
break;
case 'wednesday':
case 'thursday'://else if (day ==='wednesday' || day==='thursday')
console.log('Write code example..')
break;
...
default:
console.log('not a valid day')
}

在switch语句中,js会从符合条件的case/default代码处开始向下执行,直至遇到break退出。
我们可以通过改变break的位置来决定各个case执行的代码段。例如,一个完全没有break的switch语句,它的第一个case会依次执行所有case的代码直到switch语句的最后一行。

2-21 Expressions and Statements

表达式产生值,语句执行操作。
Expressions|表达式 是产生的代码,如2+3,true&&false, 或单纯的“1991”这样的值等。Statements|语句 是可执行的一段代码,如if else语句等,通常以;结尾。
表达式和语句的区别在于,表达式产生值,而语句进行值的转化。
如在模板字符串中,${}只能插入表达式,不能插入语句。(显然)

2-22 The Conditional (Ternary) Operator

1
2
3
4
5
6
7
8
const age =23;
//返回语句
age >=18 ? console.log('I like to drink wine') : console.log('I like to drink water');
//返回值
const drink = age >= 18 ? 'I like to drink wine' : 'I like to drink water';
console.log(drink);
//嵌入模板字符串
console.log(`I like to drink ${age>=18 ? 'wine' : 'water'}`);

2-23 Code Challenge #4

1
2
3
4
const bill = 275;
// const tip = 50 <= bill <= 300 ? bill * 0.15 : bill * 0.2; ×
const tip = bill <= 300 && bill >= 50 ? bill * 0.15 : bill * 0.2;
console.log(`The bill was ${bill}, the tip was ${tip}, and the total value is ${bill + tip}.`)
  • Title: 【UdemyJS】Section2笔记(共24节)JS基础-Part1
  • Author: Artificial-Fool
  • Created at : 2024-11-23 17:10:52
  • Updated at : 2024-12-13 18:29:37
  • Link: https://space.bilibili.com/636023121/2024/11/23/UdemyJS-note2/
  • License: This work is licensed under CC BY-NC-SA 4.0.
Comments