On JavaScript operators--conditions, commas, assignments, () and void operators _javascript tips

Source: Internet
Author: User
Tags logical operators

Front.

There are a total of 46 operators in JavaScript, and there are many operators in addition to the arithmetic, relational, bitwise, and logical operators described earlier. This article describes the conditional operator, the comma operator, the assignment operator, (), and the void operator

Conditional operator 

The conditional operator is the only ternary operator (three operands) in JavaScript, sometimes directly called the ' ternary operator '. Usually this operator is written as '?: ' and of course it's not so shorthand in code because this operator has three operands, the first operand is '? ' Before, the second operand is in '? ' and ': ', the third operand after ': '

Variable = boolean_expression? True_value:false_value;

In essence, this is based on the results of boolean_expression evaluation, decide what value to assign to the variable variable. If the evaluation result is true, assign a value to the variable variable true_value, and assign a value to the variable variable if the evaluation result is false false_value

The operands of a conditional operator can be of any type, the first operand as a Boolean value, and if it is true, the second operand is computed and its result is returned. Otherwise, if the first operand is a false value, then the third operand is computed and its results are returned. The second and third operands will always compute one of them, which cannot be performed simultaneously

In fact, using an If statement also brings the same effect, the '?: ' operator simply provides a shorthand form. Here is a typical scenario for '?: ' to determine whether a variable is defined (and to have a meaningful truth value), use it if it is defined, and use a default if there is no definition:

greeting = ' Hello ' + (username username: ' there ');

This is equivalent to the following code that uses the IF statement, but obviously the above code is more concise:

greeting = ' Hello ';
if (username)
  Greeting + = Username;
else
  greeting = ' there ';

Ternary conditional expressions have the same expressive effect as if...else statements, but they have a significant difference, If...else is a statement, there is no return value; The ternary conditional expression is an expression with a return value. So, in situations where you need to return a value, you can only use ternary conditional expressions instead of using the If...else

Console.log (true?) ' T ': ' F ');

In the code above, the parameter of the Console.log method must be an expression, and then only ternary conditional expressions can be used

Comma operator

The comma operator is a two-dollar operator, and its operands can be of any type. It computes the left operand first, then calculates the right operand, and finally returns the value of the right-hand operand, which can be executed in a single statement with a comma operator

i = 0,j = 1,k = 2;
The result is 2, which is basically equivalent to the following code
i =0 j = 1; k = 2;

A comma operator is often used to declare multiple variables

var iNum1 = 1, Inum = 2, iNum3 = 3;

The most common scenario for a comma operator is in a for loop, which typically has more than one loop variable:

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 to assign a value, 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] Removing the parentheses will cause an error

Assignment operator 

The simple assignment operator is represented by the equal sign ' = ', which assigns the value to the right of the equal sign to the variable or property to the left of the equal sign

i = O;
o.x = 1;

The ' = ' operator expects its left operand to be a left value: A variable or an object attribute (or an array element). Its right-hand operand can be any value of any type. The value of an assignment expression is the value of the right-hand operand

Although assignment expressions are usually very simple, there are times when you see a complex expression that contains an assignment expression. For example, you can put assignment and relational operators in an expression, like this:

(a=b) = = 0

If you do this, you should know the difference between the ' = ' and ' = = ' operators. ' = ' has a very low priority, usually when a long expression is used with the value of an assignment statement, you need to add parentheses to ensure the correct order of operations

The binding of an assignment operator is right-to-left, that is, if more than one assignment operator appears in an expression, the order of operations is from right to left. Therefore, you can assign values to multiple variables in the following ways:

i = j = k = 0;//Initializes three variables to 0

JavaScript also provides 11 composite assignment operators, all of which are assignment operators that specify the operation and then return the resulting value to the variable on the left

[note] The purpose of designing these operators is to simplify assignment operations and to use them without any performance improvement

Total + = Sales_Tax;
Equivalent to Total
= total + sales_tax;
   The operator example is    equivalent to
+ +     a+=b a=a+b-
=     a-=b    a=a-b
*=
a*=b a=a*b/=     a/=b    a=a/b
%=     a%=b    a=a%b
<<= a<<=b a=a<<b > >=    a>>=b   a=a>>b
>>>=    a>>>=b
a=a>>>b &=     a&=b    a=a&b
|=     a|=b    a=a|b ^= a^=b a=a^b

In most cases, an 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 row, expression a evaluates once, and in the second row, expression a evaluates to two times, and only when a contains an expression that has side effects, such as function calls and assignment operations, the two are not equivalent

data[i++]*=2;
data[i++] = data[i++]*2;

Parentheses operator 

The parentheses operator has two uses: if the expression is placed in parentheses, the action is evaluated, and if you follow the function, the function is called

Put an expression in parentheses and return the value of an expression

Console.log ((1)); 1
console.log ((' a '));//' A '
console.log ((1+2));//3

Putting the object in parentheses returns the value of the object, which is the object itself

var o = {p:1};
Console.log ((o));//Object {p:1}

Putting a function in parentheses returns the function itself. If the parentheses are immediately following the function, it means calling the function, which is to evaluate the function

function f () {return 1;}
Console.log ((f));/function f () {return 1;}
Console.log (f ()); 1

Because the parentheses function is evaluated, if you put the statement in parentheses, you will get an error, because the statement does not return a value

Console.log (var a = 1);//syntaxerror:unexpected token var
console.log (var a = 1);//syntaxerror:unexpected token Var

void operator

Void is a unary operator that appears before the operand, the operands can be of any type, the operands are calculated as usual, but the results are ignored and the undefined is returned. Because void ignores the value of the operand, void is used to make the program more semantic when the operand has side effects

Console.log (void 0); Undefined
console.log (void (0));//undefined

"Action One" replaces undefined

Because undefined is not a keyword, it is overridden in the Ie8-browser and is overridden in the scope of the higher function, so it can be replaced with void 0来 undefined

var undefined = ten;
Console.log (undefined)//ie8-browser under 10, the high version of the browser for undefined

function t () {
  var undefined = ten;
  Console.log (undefined);
}
Console.log (t ());//all browsers are 10

"Action two" client URL

This operator is most commonly used in client Url--javascript:url, where a side-effect expression can be written in the URL, while void lets the browser not display the result of the expression. For example, the void operator is often used in <a> tags in html code

<a href= "Javascript:void window.open ();" > Open a new Window </a>

"Action three" blocks default events

The way to block the default event is to return the value false to the event

General wording
<a href= "http://example.com" onclick= "f (); return false;" > Text </a>

Use the void operator to replace the above notation

<a href= "Javascript:void (f ())" > Text </a>

This article on the JavaScript operators-conditions, commas, assignments, () and void operators are small parts to share all the content, hope to give you a reference, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.