JavaScript Expressions & Operators

Source: Internet
Author: User
Tags arithmetic arithmetic operators

Arithmetic operators

If the operand (or the result of the conversion) is a Nan value, the result of the arithmetic operation is also nan.
The result of a divisor of 0 is a positive infinity or a negative infinity, and a 0/0 result is Nan, and all of these operations do not give an error.

The "+" operator
    • If one of the operands is an object, the object is converted to the original value (the valueof () conversion is preferred in addition to the Date object).
    • After the conversion of an object to the original value, if one of the operands is a string, the other operand is converted to a string, and then the string is concatenated.
    • Otherwise, the two operands are converted to a number (or Nan) and then the addition operation.
12           // 3"1"2         // "12",数字转换为字符串1 + {}          // "1[object Object]",对象转换成字符串后进行连接truetrue     // 2,布尔转换为数字2null        // 2,null转换为02undefined   // NaN,undefined转换为NaN后做加法

The synchronization of the result of the operation depends on the operator's order of operations, such as:

12"blind mice";   //"3 blind mice"1 + (2"blind mice"//"12 blind mice"


The "+ +" operator

The expression ++x does not sum x=x+1 exactly the same.
If x is the string "1", the result of ++x is the number , while x+1 is the string * "11".

Relational operator "= = =" (Strict equality)

The strict equality operator, which does not have any type conversions, compares the following procedures:

    • If the two value types are different, they are not equal.
    • If two values are numeric and the values are equal, they are equal. If one is 0 and the other is-0, then they are equally equal.
    • If two values are strings and the 16 digits on the corresponding bits contained are exactly equal, they are equal.
    • If two values are Boolean true or false, they are equal.
    • If two values are null or both are undefined, they are equal .
    • If one of the values is Nan, or if two values are Nan, they are not equal .
    • If two reference values point to the same object, array, or function, they are equal . If you point to different objects, they are unequal, although two objects have exactly the same properties.

"= =" (equality operator)
    • If the two operands are of the same type, the comparison is made by strict equality rule = = =.
    • If the two operands are of a different type, the following type conversions occur:

If one value is null and the other is undeined, then they are equal .
If one value is a number and the other is a string, then string–> number is converted to a value comparison.
If a value is a Boolean type, then true–> 1, false–> 0 is converted to a value comparison.

"1"true;    //true


comparison operator (<,;, <=, >=)
    • If the operand is an object, the object is first converted to the original value.
    • After the object is converted to the original value, if the two operands are strings , the comparison is by string.
    • If at least one operand is not a string, it is converted to a number for comparison. 0 and 0 are equal, and if one of the operands is Nan, the comparison operator always returns false.
"11""3"  true,字符串比较"11"3    false,"11"转换为11"one"3   false,"one"转换为NaN


In operator

The in operator expects that its left operand is a string or can be converted to a string, and the right operand is an object.
The expression returns true if the object on the right has a property name called the left operand value.

var point = {x:1,y:1};"x" inchPoint// true, the object has a name of"x"of the properties"Z" inchPoint// false, the object does not exist with the name"Z"of the properties"ToString" inchPoint// true, the object contains the ToString () method var data = [7,8,9];"0" inchData// true, the array contains the elements"0"1 inchData// true, the number is converted to a string3 inchData// false, no index is3The Elements


instanceof operator

In order to calculate the expression "O instanceof F", JavaScript first computes the F.prototype and then finds O in the prototype chain (prototype chain), and if found, then O is an instance of f (or the parent of f), and the expression returns TRUE.

Logical expression logic or (| |)

This operator is often used to select the first truth expression from a set of alternative expressions:

// 如果max_width已经定义了,则直接使用它// 否则在preferences对象中查找max_width// 如果对象中也找不到,则使用一个常量varmax=|| preferences.||500;


Other operator delete operators

Delete is a unary operator used to delete an object property or array element (but not necessarily free memory), and the original value becomes undefined.

var o = { x:1, y:2 };delete o.x;"x"in o;               // false,属性已经被删除var a = [1,2,3];delete a[2];2in a;                 // false,数组元素2已经被删除a.length;               // 3,注意:数组的长度没有改变!!!


void operator

This operator is often used to avoid output values that should not be output, such as <a> when invoking JavaScript functions from elements of HTML. To do this correctly, the function cannot return a valid value, otherwise the browser will empty the page and show only the result of the function. For example:

<a href="javascript:window.open(‘about:blank‘)">Click me</a>

If you put this line of code in an HTML page, click on the link to see "[Object]" on the screen.
This is because the window.open () method returns a reference to the newly opened window. The object is then converted to the string to be displayed.
To avoid this effect, you can call the window.open () function with the void operator:

<a href="javascript:void(window.open(‘about:blank‘))">Click me</a>

This causes the window.open () call to return undefined, which is not a valid value and will not be displayed in the browser window.

JavaScript Expressions & Operators

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.