【UdemyJS】Section2笔记(共24节)JS基础-Part1
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登录弹窗),可以输入信息并返回字符串。
- 之前学python时经常用“==,而在js中应以“===”平替。Javascript的双等号规则太过鬼畜,一般不建议使用。
2-17 Logical Operators
- and(与): A && B
- or(或): A || B
- not(非): !A
1
2
3
4
5
6
7
8const 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 | const day = 'monday' |
在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 | const age =23; |
2-23 Code Challenge #4
1 | const bill = 275; |
- 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.