Operators in JavaScript
JS operators
To perform various operations, different operation symbols must be used.
## Arithmetic operators: +, -, *, /, %, ++, --
(1) "%" remainder operator, divide two numbers and get the remainder.
A = 10 % 3; // A = 1, if the remainder is not 0, then the two are not divisible A = 10 % 2; // A = 0, If the remainder is 0, then the two numbers can be divided(2) "++" plus 1 operator, self-increment 1
"++" can be used as a prefix (++i) or as a suffix (i++).<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>php.cn</title> <script> var a = 1; var b = 1; document.write(++a); document.write("<hr>") document.write(b++); </script> </head> <body> </body> </html>
Observe the output of the above example
When ++a, no matter what, a=a+ will be executed first 1. Then perform output and other operations, such as page output and calculations.
When b++ is used, other operations, such as output and calculations, will be performed first. Class, finally when this statement ends, execute a=a+1, and then this statement ends
(3) "--" minus 1 Operator, decrement by 1
"--" can be used as a prefix (--i) or as a suffix (i--). The "--" example is the same as the "++" example. Please try and test it yourself.Assignment operators: =, +=, -=, *=, /=
##String operators: +, + =
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>php.cn</title>
<script>
var name = "php.cn";
var str = "歡迎來(lái)到"+name;
document.write(str);
</script>
</head>
<body>
</body>
</html>
Comparison operators: >, <, >=, <=, ==, !=, ===, !==
The result of the comparison operator is a Boolean value (true or false).A = 10 > 20; // Result A=false
A = 20>=20; // Result A=true
A = 10 %2 == 0; // Result A=true
A = 10%2 == “0”; // Result A=true
A = 10%3 != 0; // Result A=true
A = 10%2 === “0”; //Result A=false
Note:
"==" equals. Only the values ??of two variables are compared, regardless of type. As long as the values ??are the same, it returns true, otherwise it returns false.
"===" are all equal. Both compare variables and determine types. If the type and value are the same, return true, otherwise return false. Logical operators: &&, ||, ! The logical operator has two results: true or false. "&&" logical AND (and relationship). If both left and right operands are true, the result is true, otherwise, the result is false. Logical AND means that when two conditions are met at the same time, the result is true. "||" logical OR. As long as one of the two conditions on the left and right is met, true is returned, otherwise false is returned. "!" negation operation. !true = false , !false = true , !100 = false Ternary operator: ?: The so-called "ternary operator" refers to three operands. Syntax: Conditional expression? Result 1: Result 2 Syntax: Operand 1? Operand 2: Operand 3 Meaning: If the condition is true, then Execute the code for "Result 1"; if the condition is false, execute the code for "Result 2". Actually: the ternary operator is the modified form of if else. (if else we will study in the next chapter)<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>php.cn</title>
<script>
//比較字符串?dāng)?shù)值和數(shù)值
var name1 = "520";
var name2 = 520;
document.write(name1==name2);
document.write("<hr>");
document.write(name1===name2)
</script>
</head>
<body>
</body>
</html>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>php.cn</title>
<script>
//給一個(gè)成績(jī)
var score=61;
//判斷成績(jī)所屬級(jí)別
if(score<60){
document.write("對(duì)不起,您沒(méi)有及格");
}else if (score>=60&&score<70){
document.write("您剛好及格");
}
</script>
</head>
<body>
</body>
</html>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>php.cn</title>
<script>
var age=79;
if(age<10||age>60){
document.write("您好,您符合我們店的優(yōu)惠條件,今天買東西全場(chǎng)5折");
}else if (age>=10&&age<=60){
document.write("不好意思,您不符合我們店的優(yōu)惠條件,今天買東西不享受折扣");
}
</script>
</head>
<body>
</body>
</html>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>php.cn</title>
<script>
var a=true;
document.write(a);
document.write("<br/>");
document.write(!a);
</script>
</head>
<body>
</body>
</html>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>php.cn</title>
<script>
var a=10;
var b=20;
//判斷a和b那個(gè)大,把大的那個(gè)賦值給max
var max=a>b?a:b;
document.write("最大值:"+max);
</script>
</head>
<body>
</body>
</html>