Expressions are formulas used for calculations when JavaScript scripts are running, and can include constants, variables, and operations. Operator;
Arithmetic operator
+-*/% ++ --
Note: Numeric types support increment and decrement operators, Boolean types support increment and decrement operators, strings do not support increment and decrement operators, null supports increment and decrement, undefined does not support
Character connector
The + sign is used to connect two strings;
As long as one of the + connection operands is a string type, js will automatically convert the non-string into a string type for processing ;
The order of js code execution is from left to right, so in the + expression, before string data is encountered, all numerical data that appears (or can be converted into numerical strings) is still is treated as a numerical value. In order to avoid this situation, we can add an empty string;
Assignment operator
=, +=, -+, * =, /=, %=
+= can be used to connect strings;
Comparison operators
==, ===, 》=,《=,! =,! ==
The result of comparison operator is Boolean;
== only compares the values for equality, === compares
Logical operator
&&, ||,!
&&: If the first result is false and the entire expression is false, the second one is short-circuited.
&&: There must be two expressions, and both expressions are true, then the result is true;
||: If one of the two expressions is true, the result is True;
||: If the first expression is true, the entire result is true, short-circuiting the second expression;
Ternary operator
exp1? exp2: exp3;
Other operators
Comma operator: Comma is used to connect multiple expressions into one expression, and the value of the new expression is the last The value of an expression is mostly used in variable declarations;
var z=(a=1,b=2,c=3);
then z=3; take the last expression The value of the expression;
void operator: The void operator is used to indicate that an expression has no return result;
var z=void(a=1,b=2,c=3) ;
alert(z);
then returns undefined
typeof operator: The typeof operator is used to return a string and the data type of the operand;
Detect variable type;
Related recommendations:
Detailed explanation of js expressions and operator codes
The above is the detailed content of Detailed explanation of js expressions and operator examples. For more information, please follow other related articles on the PHP Chinese website!