JavaScript basics day 02
1-operator (operator)
1.1 Classification of operators Operators, also known as operators, are symbols used to implement functions such as assignment, comparison, and performing arithmetic operations.
Arithmetic operators, increment and decrement operators, comparison operators, logical operators, assignment operators
1.2 Overview of arithmetic operators
Concept: Symbols used in arithmetic operations to perform arithmetic operations on two variables or values.
1 |
Commonly used operators in JavaScript are: Arithmetic operators, increment and decrement operators, comparison operators, logical operators, assignment operators |
The accuracy of
floating-point numbers The highest accuracy of floating-point numbers is 17 decimal places, but its accuracy is far inferior to integers when performing arithmetic calculations.
So: don't directly judge whether two floating-point numbers are equal!
Expressions and return value expressions: are combinations of numbers, operators, variables, etc. that can be calculated in a meaningful arrangement of values. Simple understanding: numbers, operations The formula expression composed of characters, variables, etc. will eventually have a result, which is returned to the developer, called the return value
1 2 |
var result = 0.1 + 0.2; // The result is not 0.3, but: 0.30000000000000004 console.log(0.07 * 100); // The result is not 7, but: 7.000000000000001 |
1.3 Increment and decrement operators Overview of increment and decrement operators If you need to repeatedly add or subtract 1 to a numeric variable, you can use the increment (++) and decrement (--) operators to complete. Increment operator Pre-increment operator The post-increment operator 1.4 Comparison Operators Overview of Comparison Operators Concept: A comparison operator (relational operator) is an operator used when comparing two data. After the comparison operation, a Boolean value (true / false) is returned as the result of the comparison operation.
Equal sign comparison |
||||||
1 2 |
console.log(18 == '18'); console.log(18 === '18'); |
1.5 Overview of logical operators
Concept: Logical operators are operators used to perform Boolean operations, and their return values are also Booleans. It is often used to judge multiple conditions in later development
Logical AND &&
both sides are true to return true, otherwise it returns false
Logical OR ||
Both sides are true before returning true, otherwise it returns false
Logical negation! Short-circuit operation (logical interruption) The principle of short-circuit operation: when there are multiple expressions (values), when the value of the expression on the left can determine the result, the operation of the expression on the right is not continued. Value; console.log( 123 && 456 ); // 456 console.log( 0 && 456 ); // 0 console.log( 123 && 456&& 789 ); // 789 逻辑或
1 2 3
|
||||||
1 2 3 |
console.log( 123 || 456 ); // 123 console.log( 0 || 456 ); // 456 console.log( 123 || 456 || 789 ); // 123 |
1.6 赋值运算符
1 |
概念:用来把数据赋值给变量的运算符。 |
1 2 3 4 |
var age = 10; |
1.7 运算符优先级
一元运算符里面的逻辑非优先级很高 逻辑与比逻辑或优先级高
2 - 流程控制
2.1 流程控制概念
1 2 3 4 5 |
在一个程序执行的过程中,各条代码的执行顺序对程序的结果是有直接影响的。很多时候我们要通过控制代码的执行 顺序来实现我们要完成的功能。 简单理解:**流程控制就是来控制代码按照一定结构顺序来执行** 流程控制主要有三种结构,分别是**顺序结构**、**分支结构**和**循环结构**,代表三种代码执行的顺序。 |
2.2 顺序流程控制 顺序结构是程序中最简单、最基本的流程控制,它没有特定的语法结构,程序会按照代码的先后顺序,依次执行,
程序中大多数的代码都是这样执行的。
2.3 分支流程控制 分支结构
由上到下执行代码的过程中,根据不同的条件,执行不同的路径代码(执行代码多选一的过程),从而得到 不同的结果
1 |
JS 语言提供了两种分支结构语句:if 语句、switch 语句 |
if 语句 语法结构
1 语句可以理解为一个行为,循环语句和分支语句就是典型的语句。一个程序由很多个语句组成,一般情况下, 会分割成一个一个的语句。
执行流程
if else语句(双分支语句) 语法结构
1 2 3 4 |
// 条件成立执行代码,否则什么也不做 if (条件表达式) { // 条件成立执行的代码语句 } |
执行流程 if else if 语句(多分支语句) 语法结构
执行逻辑 |
||||
2.4 三元表达式 语法结构 执行思路 如果表达式1为 true ,则返回表达式2的值,如果表达式1为 false,则返回表达式3的值 简单理解: 就类似于 if else (双分支) 的简写 2.5 switch分支流程控制 语法结构 switch 语句也是多分支语句,它用于基于不同的条件来执行不同的代码。当要针对变量设置一系列的特定值 的选项时,就可以使用 switch。
switch :开关 转换 , case :小例子 选项 |
如果存在匹配全等(===) ,则与该 case 关联的代码块会被执行,并在遇到 break 时停止,整个 switch 语句代 码执行结束 如果所有的 case 的值都和表达式的值不匹配,则执行 default 里的代码 一般情况下,它们两个语句可以相互替换 |