JavaScript Learning (4.10): conditional operator (?:), typeof operator, delete operator, void operator, comma operator (,) __javascript conditional operator

Source: Internet
Author: User
Tags array length
4.13 Other operators
The 4.13.1 conditional operator (?:) condition operator is the only ternary operator in JavaScript. Its operands can be of any type. The first operand is a Boolean value, and if it is true, the second operand is computed and the result is returned; otherwise, if the first operand is a false value, then the third operand is computed and the result is returned.
The 4.13.2 typeof operator TypeOf is a unary operator, placed in front of its single operator, which can be of any type. The return value is a string representing the operand type.
  x                                                typeof x undefined                                    undefined null         &N Bsp                                   &NB Sp;object true/false                             &NB Sp       boolean  any number or Nan                       & nbsp    number arbitrary string                           &NBSP ;       string arbitrary function                         &NBSP ;            function any built-in object (non function)                object arbitrary host pair Like                                 right compiler implementation String, but not undefined,boolean,number and string
typeof commonly used in writing: typeof xxx or typeof (XXX)
When the operand is null, typeof returns "Object" and must be explicitly instrumented for special values if you want to separate the null from the object area. For a host object, typeof may not return "object" and return a string. In fact, most host objects in client JavaScript are of type "object". The result of typeof operations for all objects and arrays is "object" instead of "function", so it is helpful to differentiate objects and other original values. If you want to differentiate between classes of objects, you can use the instanceof operator. A function in JavaScript is one of the objects, but the typeof operator treats the function special and returns a special value "function" on the function. In JavaScript, there is a subtle difference between a function and an executable object. All functions are executable, but the object may also be executable and can be called like a function, but it is not a real function. In ECMAScript 3, the typeof operator returns "function" for all built-in executable objects. Ecmasript 5 expands to all executable objects, including built-in objects and host objects.
The 4.13.3 delete operator      delete is a unary operator that is used to delete a cashing attribute or an array element. Like an assignment, increment, and decrement operator, delete has a side effect that is used to delete operations, not to return a value:
var o = {x:1, y:2}; Delete o.x;  //true "x" in O;&N bsp;  //false
var a =[1, 2, 3]; Delete a[2]; //true "2" in a; //false element 2 no longer exists in the array a.length;  //3  But the array length has not changed a[2]; //undefined  and set a undefined
    Delete want its operand to be a left value if it is not a left , then delete will return true without doing anything, or delete will attempt to delete the specified left value. If the deletion succeeds, delete returns True. However, some built-in core and client properties cannot be deleted, and the variables declared by the user through the Var statement cannot be deleted, and functions and function parameters defined through the function statement cannot be deleted.       in Ecmasript 5 strict mode, the delete operation throws a syntax error (SYNTAXERROR) exception if the operand of the delete is illegal, such as a variable, function, or function argument. This works only if the operand is a property expression. In strict mode, delete throws a type error exception when deleting a property that is not configurable. In non-strict mode, these delete operations do not complain, but simply return false to indicate that the operand cannot perform a delete operation.
var o = {x:1, y:2}; Delete o.x//true: Deletes an object property typeof o.x;//undefined property does not exist delete o.x;//true: Deletes a nonexistent property delete o;  /should not be deleted at this time, return false, strict mode, throw an exception   but the browser has a different definition, chrome is true can delete o.x;
4.13.4 void operator Void is a unary operator that appears before the operand, and the operand can be of any type.     Action: The operands are calculated as usual, but ignore the calculation and return undefined, because void ignores the value of the operand, so use void to make the program more semantic when the operand has side effects. This operator is most commonly used in the client's Url-javascript:url, which can write an expression with side effects in the URL, while void lets the browser not display the result of the expression: <a href= "javascript:void window.open (); " > Open a new Window </a>
The 4.13.5 comma operator (,) comma operator is a two-dollar operator, and its operands can be of any type.  It calculates the left operand first, then calculates the right operand, and finally returns the value of the right-hand operand: i=0, j=1, z=2; 2 equivalence i = 0; j = 1;     z = 2; The expression on the left is always evaluated, but the result is ignored, that is, only the left expression has side effects, and the comma operator is used to make the code more smooth. The most common scenario for a comma operator is in a for loop, which typically has more than one loop variable.
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.