The previous words
There are 46 operators in JavaScript. In addition to the arithmetic operators, relational operators, bit operators, and logical operators mentioned earlier, there are many operators. This article will introduce conditional operators, comma operators, assignment operators, () and void operators
Conditional operator
The conditional operator is the only ternary operator (three operands) in JavaScript, and is sometimes called a 'ternary operator' directly. Usually this operator is written as '?:', of course, it is often not abbreviated in code, because this operator has three operands, the first operand is before '?', the second operand is between '?' and ':', and the third operand is after ':'
variable = boolean_expression ? true_value : false_value;
Essentially, this is based on the result of evaluating boolean_expression, which determines what value to the variable variable. If the evaluation result is true, then the variable variable variable is assigned true_value; if the evaluation result is false, then the variable variable is assigned false_value_value
The operand of the conditional operator can be of any type, the first operand is treated as a Boolean value, and if it is true, the second operand will be calculated and its calculation result will be returned. Otherwise, if the first operand is a false value, the third operand is calculated and its calculation result is returned. The second and third operands always calculate one of them, and it is impossible to execute both at the same time.
In fact, using if statements will also bring the same effect, the '?:' operator only provides a shorthand form. Here is a typical application scenario for '?:', to determine whether a variable is defined (and has a meaningful truth value), use it if there is a definition, and use a default value if there is no definition:
greeting = 'hello ' + (username ? username : 'there');
This is equivalent to the code below using the if statement, but obviously the above code is more concise:
greeting = 'hello ';if(username) greeting += username;else greeting += 'there';
The ternary conditional expression has the same expression effect as the if...else statement, but there is a significant difference between the two. If...else is a statement and has no return value; the ternary conditional expression is an expression and has a return value. Therefore, in cases where the return value is required, you can only use ternary conditional expressions, and not if...else
console.log(true ? 'T' : 'F');
In the above code, the parameter of the console.log method must be an expression, and only ternary conditional expressions can be used.
Comma operator
The comma operator is a binary operator, and its operand can be of any type. It first calculates the left operand, then calculates the right operand, and finally returns the value of the right operand. Use the comma operator to perform multiple operations in a statement
i = 0,j = 1,k = 2;//The calculation result is 2, which is basically equivalent to the code below i =0; j = 1; k = 2;
Comma operators are often used to declare multiple variables
var iNum1 = 1, iNum = 2, iNum3 = 3;
The most common scenario for comma operators is in a for loop, which usually has multiple loop variables:
//The first comma in the for loop is part of the var statement //The second comma is the comma operator //It puts two expressions (i++ and j--) in a statement for(var i=0, j=10;i<j;i++,j--){console.log(i+j);}The comma operator can also be used for assignment, and when used for assignment, the comma operator always returns the last item in the expression.
var num = (1,2,3,4,5);console.log(num);//5
[Note] Remove the brackets and will report an error
Assignment operator
A simple assignment operator is represented by the equal sign '=', which assigns the value to the right of the equal sign to the variable or attribute on the left of the equal sign.
i = o;ox = 1;
The '=' operator expects its left operand to be an lvalue: a variable or object attribute (or array element). Its right operand can be any value of any type. The value of the assignment expression is the value of the right operand
Although assignment expressions are usually very simple, sometimes you will still see some complex expressions containing assignment expressions. For example, you can put assignment and relational operators in one expression, like this:
(a=b) == 0
If you do this, you should know clearly the difference between the '=' and '==' operators. '=' has very low priority. Usually when a value of an assignment statement is used in a longer expression, parentheses need to be added to ensure the correct order of operations.
The binding nature of the assignment operator is from right to left, that is, if multiple assignment operators appear in an expression, the operation order is from right to left. Therefore, multiple variables can be assigned in the following way:
i = j = k = 0;//Initialize three variables to 0
JavaScript also provides 11 compound assignment operators. These compound assignment operators are all specified operations first, and then return the obtained value to the variable on the left.
[Note] The purpose of designing these operators is to simplify assignment operations, and using them will not bring any performance improvements.
total += sales_tax;//equivalent to total = total + sales_tax;
The operator example is equivalent to += a+=ba=a+b-= a-=ba=ab*= a*=ba=a*b/= a/=ba=a/b%= a%=ba=a%b<<= a<<=ba=a<<b>>= a>>=ba=a>>b>>=a>>=a>>>=a>>>b&= a&=ba=a&b|= a|=ba=a|b^= a^=ba=a^b
In most cases, the expression is:
a op= b
Here op represents an operator, and this expression is equivalent to the following expression
a = a op b
In the first line, expression a is calculated once, and in the second line, expression a is calculated twice. Only when a contains expressions with side effects (such as function calls and assignment operations) will the two be inequality
data[i++]*=2;data[i++] = data[i++]*2;
Bracket operator
There are two uses of the bracket operator: if the expression is placed in parentheses, the function is to evaluate; if it is followed by a function, the function is to call the function.
Put the expression in parentheses and return the value of the expression
console.log((1)); //1console.log(('a')); //'a'console.log((1+2)); // 3Putting the object in parentheses will return the value of the object, that is, the object itself
var o = {p:1};console.log((o));// Object {p:1}Putting the function in parentheses returns the function itself. If the parentheses follow the function, it means calling the function, that is, evaluating the function
function f(){return 1;}console.log((f));// function f(){return 1;}console.log(f()); // 1Since the purpose of parentheses is to evaluate, if the statement is placed in the parentheses, an error will be reported because the statement does not return the value
console.log(var a = 1);// SyntaxError: Unexpected token varconsole.log((var a = 1));// SyntaxError: Unexpected token var
void operator
void is a unary operator. It appears before an operand. The operand can be of any type. The operand will be calculated as usual, but the calculation result is ignored and undefined is returned. Since void ignores the value of operands, use void when operands have side effects to make the program more semantic
console.log(void 0); // undefinedconsole.log(void(0)); // undefined
【Function 1】Replace undefined
Since undefined is not a keyword, it will be rewritten in IE8-browser and in the scope of higher-version functions; therefore, undefined can be replaced with void 0
var undefined = 10;console.log(undefined);//IE8-It is 10 under the browser, and undefined function t(){ var undefined = 10; console.log(undefined);}console.log(t());//It is 10 under all browsers【Role 2】Client URL
This operator is most commonly used in client URLs - javascript:URL. Expressions with side effects can be written in the URL, while void allows the browser to avoid displaying the calculation results of this expression. For example, the void operator is often used in the <a> tag in HTML code
<a href="javascript:void window.open();">Open a new window</a>
【Effect Three】Block default events
The way to block the default event is to set the return value to false.
//General writing method <a href="http://example.com" onclick="f();return false;">text</a>
Use the void operator to replace the above writing method
<a href="javascript:void(f())">text</a>
The above article briefly discusses javascript operators - conditions, commas, assignment, () and void operators are all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.