JavaScript statements and functions

1. Expression and operator

1. An expression operates on one or more variables or values ​​(operands) and returns a new value

2.Operators can be divided into the following categories:

        ​ ​ 2.1: Assignment operator: =

        2.2: Arithmetic operators: +, -, *, /, %, ++, --, - (negation)

        ​ ​ 2.3: Combining operators: *=, /=, +=, -=, %=

        ​ ​ 2.4: Comparison operators: ==, !=, >, >=, <, <=, ===, !==

        ​ ​ 2.5: Logical operators: &&, ||, !

        2.6: String operations: +

3. "===" Absolutely equal: the data types are consistent and the values ​​are equal.

4. "!==" Not absolutely equal: data types are inconsistent or values ​​are not equal

        4.1 Inconsistent data types

        4.2 Values ​​are not equal

        4.3 The data types are inconsistent and the values ​​do not want to wait

Here's how to use the operators:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title></title>
	</head>
	<body>
		<script type="text/javascript">
			//" == " 表示等于     " != "表示不等于
			var a = ( 15 % 2 == 0)// 结果为false
			var b = ( 15 % 2 != 0)//结果为true
			//控制台输出结果
			console.log(a)
			
			//自增 " ++ "	自减 " -- "
			var i = 1
			console.log(i++) //结果为1	i++先输出本身在加1
			console.log(++i) //结果为3	++i先自加1后输出
			
			var j=10
			j = j+10 // 第一种方法
			j += 10  //第二种方法
			//两种方法运算结果都一样
			console.log(j)
			
			//1-3之间的所有整数之和
			var result = 0
			result = result + 1
			result = result + 2
			result = result + 3
			result += i
			
			//==	===
			console.log(10 == "10") //结果为true 
			//因为 " == " 运行时会自动把两个值转换为相同类型的值
			
			console.log(10 === "10") //结果为false
			console.log("10" === "10") //结果为true 
			/* 因为 "===" 运行时不仅会判断是否为数据是否相等,
			还会判断他们的数据类型是否相同 
			"===" :绝对等于:数据类型一致,数值相等*/
			
			console.log("10" !== "10") //结果为false
			
		</script>
	</body>
</html>

2. if branch structure

1. Simple if statement

if("conditional expression"){ Statement block! }

As shown below:

 2.if...else statement: You can use this statement if you need two results.

if("conditional expression"){ Statement block! } else { Statement block! }



As shown below:

 3. Multiple if statements: Use this statement when making multiple judgments

Example: If your JS score is above 80 points, you are very suitable for development. If your score is between 60-80, you are suitable for learning software implementation. If your score is below 60 points, then you are suitable for new media.

The implementation code is shown below:

 4. Nested if statements

Example: Use nested branches to find the maximum value of three numbers

The implementation code is shown below:

 

3. switch branch statement

In a switch construct, automatic type conversion is ignored i.e. the expression must be absolutely equal to a constant

switch statement format:

 Example: Simulate an automatic beverage machine. When you enter any integer from 1 to 5, the corresponding beverage will be given and prompted whether to continue purchasing the beverage.

The implementation code is as follows:

 

Guess you like

Origin blog.csdn.net/m0_71385552/article/details/126858402