JavaScript operators
JavaScript operators mainly include:
Arithmetic operators
Assignment operators
Comparison operators
Ternary operator
Logical operator
String concatenation operator
Arithmetic operators
Description Operator ##+ Add y = 2+1 y = 3
- Subtract y = 2-1 y = 1
##For pre-add and post-add, the result after execution is the variable plus 1. The difference is that the return result is different during execution. Please refer to the following two examples:
var x = 2;alert(++x); //Output: 3
alert(x); //Output: 3var y = 2;alert(y++); / /Output: 2
alert(y); //Output: 3
##Assignment operator
Assignment operator = is used for assignment operations. The assignment operator is used to assign the value on the right to the variable on the left. Set y = 6, see the table below:
Example is equivalent to
Operation result
##= y = 6 empty y = 6 += y += 1 y = y+1 y = 7 -= y -= 1 y = y-1 y = 5 *= y *= 2 y = y*2 y = 12 /= y /= 2 y = y/2 y = 3 %= y %= 4 y = y%4 y = 2 ##Comparison operations Symbol Operator Explanation Example Operation result == Equal to 2 == 3 FALSE 2 === "2" FALSE ) ternary operator x = 2;y = (x == 2) ? x : 1; Logical Operators Operator Explanation Example Operation result && Logical AND (and) x = 2; y = 6; x && y > 5 FALSE || Logical OR (or) x = 2; y = 6; The opposite of x = 2; y = 6; !(x > y) TRUE The connection operator + is mainly used to connect two strings or string variables. Therefore, when you use this operator on strings or string variables, you are not adding them. x = "beijing"; // To add spaces between two strings, you need to insert spaces into a string: x = 25;
alert(y); //Output: 2
y = x + "Hello!"; //Result: y = "beijing you OK! "
When performing the concatenation (addition) operation on strings and numbers, the numbers will be converted into strings and then concatenated (added): <!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php中文网(php.cn)</title>
</head>
<body>
<button onclick="myFunction()">点击这里</button>
<p id="demo"></p>
<script>
function myFunction()
{
txt1="What a very";
txt2="nice day";
txt3=txt1+txt2;
document.getElementById("demo").innerHTML=txt3;
}
</script>
</body>
</html>
- Course Recommendations
- Courseware download
The courseware is not available for download at the moment. The staff is currently organizing it. Please pay more attention to this course in the future~ 














