JavaScript (7.6) operator

Source: Internet
Author: User

One, the conditional operator (?:)

The conditional operator is the only ternary operator in JavaScript, sometimes called a "ternary character". Usually this operator is written as "?:" and certainly not in the code, because the operator has three operands and the first operand is "?". Before, the second operand is in the "?" and ":", the third operand after ":", for example:

x > O? X:-X//To find the absolute value of X

The operand of the conditional operator can be any type. The first operand is treated as a Boolean, and if it is true, then the second operand is computed and the result of its calculation is returned. Otherwise, if the first operand is a false value, then the third operand is computed and the result of its calculation is returned. The second and third operands always calculate one of them and cannot be executed simultaneously.

The same effect is achieved with an if statement, and the "?:" operator simply provides a shorthand form. Here is a typical scenario for a "?:" that determines whether a variable has a definition (and has a meaningful truth value), uses it if it has a definition, and uses a default if none is defined:

Greeting = "Hello" + (username? Username: "There");

This is equivalent to the following code that uses the IF statement:

Greeting = "Hello"; if (username) Greeting +=username;else Greeting + = "there";

Second, typeof operator

A typeof is a unary operator placed before its individual operand, which can be of any type. The return value is a string representing the type of operand.

The most common usage of typeof is written in an expression, such as:

Third, delete operator

Delete is a unary operator that is used to delete object properties or array elements. Just like an assignment, increment, increment operator, delete has side effects, and it is used to delete operations, not to return a value.

var o = {X:1,y:2};          Define an object delete o.x;    Deleting an attribute "X" in O//results will return a false because the attribute does not exist in the object var a = [x-ray];        Defines an array of delete a[2]; Delete the third element in the array 2 in a//the result still returns false because this element does not exist however, if the input a.length, the length of the array is still 3, does not change, although this element has been deleted, but the deletion left a "black hole", and this "black Hole "still occupies a length, so the length of the array does not change.

Four, void operators

Void is a unary operator that appears before the operand and can be of any type. This operator is not used frequently: operands are evaluated as usual, but the results are ignored and 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.

PS: This operator is most commonly used in the client's URL----javascript:url, which can write expressions with side effects in the URL, while void lets the browser not show the results of this 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>

Of course, it is clearer to bind an event handler to <a> 's onclick than to write "Javascript:url" in the href, so that the operator void becomes dispensable.

Five, comma operator (,)

The comma operator is a two-tuple operator whose operands can be of any type. It first calculates the left operand, calculates the right operand, and finally returns the value of the right operand.

The basic syntax is this:

i=0,j=1,k=2;

The result of the calculation is 2, which is basically equivalent to the following code:

i = 0;j = 1; K = 2;

It always calculates the expression on the left, but the result of the calculation is ignored, that is, only the left expression has side-effects, and the comma operator is used to make the code more fluent. The most common scenario for a comma operator is in a for loop, which typically 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 one (for loop) statement for (var i =0,j =10;i<j;i++,j--) Console.log (I+J);


JavaScript (7.6) operator

Related Article

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.