A unary operator in JS

Source: Internet
Author: User

    • An expression
    • Unary operator
    • Priority level
    • Binding nature
    • Order of Operations

What is an expression?

is a phrase in JS, the interpreter encountered this phrase will be to calculate it, get a result to participate in the operation, we have to participate in the operation of the various phrases called expressions. In fact, JS code is composed of expressions and operators, you can say that in addition to the operator is basically an expression.

For example: A + 1; In this line of code, the plus sign to the left of a and 1 on the right is the expression, and the plus sign is the operator.

Classification of expressions (probably divided into 6 categories)

1, original expression (4 kinds): constant, variable, direct amount, keyword

    • Constants: The quantities that do not change. In fact, most of the things in JS can be changed, if we want to let a thing remain unchanged, for example, about the calculation of the pi in the circle, you can set it to a constant, the constants are usually used in uppercase letters, or underlined;
      Const PI = 3.14= 3;  // changing the value of a constant will cause an error
    • Variable: Can change at any time (this is not the same as the underlying data type of JS, not matter)
      var a = 1= 2;  // re-assign a value to variable a
    • Direct Volume: number, string, regular expression, such as: 1 + 1; Here 1 is a direct quantity;
    • Keywords: All the keywords are expressions;

2, initialize the expression:

    • var obj = {name: ' Suki};
    • var arr = [1, 2, 3];

3, Function-definition expression: Functions foo () {}

4, Function call expression: foo ();

5, property access expression: Obj.name; Obj[name];

6, Object creation expression: New Foo ();

Unary operator

You can only manipulate the symbols of one expression, a total of 9;

One dollar Plus

    • The number of a unary plus operation, the result returns the number itself, to pay attention to negative numbers for a unary plus operation results or negative, do not think you will get an integer;
    • A unary plus operation on a Boolean type, TRUE returns 1,false returns 0;
    • A unary plus operation on NULL, returning 0;
    • A unary plus operation is performed on the undefined, returning nan;
    • A unary plus operation on a string, which has two results, returns 1 if the string is composed of a pure number, or Nan if not;
    • A unary plus operation on the object, there are two results, one is to return the number, and the second is to return Nan, the operation of the first call the ValueOf method, if the number results are not obtained, call the ToString method, and then the resulting string to a unary plus operation;

Purpose: Converts an expression into a number;

One Yuan minus

    • Use for numbers: Returns the negative numbers of the digits;
    • For other data types use: First try to convert the expression to a number, and then take negative numbers;
      var false ; -B;  // -0

First convert false into a number to get 0, and then take 0 negative numbers to get-0, in JS +0 = = = 0;//true;

Predecessor increment, decrement operator

The interpreter first adds one or more actions to the expression, and then lets the expression participate in the subsequent operation;

var num1 = 2; var num2 =; --num 1 + num2;  // 1 +

Post increment, decrement operator

Let the expression participate in the operation, and then add one or minus one of the expression, the usual post-increment, decrement operator used a little more;

var num1 = 2; var num2 =+ num2;  // 2 +num1;  // 1

var age = +; ++age;  //  A // if the operand is a number, the two are the same: age = age + 1;  //  A

But when it comes to string expressions, things are different.

var name = ' Suki '; ++name;  // NaN // as long as one operand is a string, the plus sign becomes the string connector name = name + 1;  // ' Suki1 '

Priority level

As in the mathematical expression, the first calculation multiplication plus minus, because the priority of multiplication is higher than the addition and subtraction;

Property access > Unary operators > Multiplication > Add and subtract > Compare > Equals > and Operations > or Operations > Three mesh Operations > Assignment operations

Do not remember so much, just remember the following three points is good:

    • The property access expression has the highest priority;
    • The assignment operation (=) has the lowest priority (in fact, the comma is lower than the equal sign, but the usual comma does not participate in the operation);
    • In all operators, the unary operator has the highest priority;

var a = 3; // false;

Because the precedence of equal (= =) is lower than the increment operator (unary operator), the ++a is computed first, the result is 4, then the 4==3 is computed, and the return is false;

Binding nature

How is it calculated when there are multiple operators in a line of code, and the precedence of these operators is equal? This leads to the problem of integration;

    • Left combination
    • Right combination

In fact, from the left to calculate or from the right to start counting the problem;

All unary operators, trinocular operators, and assignment operators are right-associative, and the other operators are left-associative;

!a++;

If the operation is from left to right,!a first gets a Boolean type, (!a) + + finally gets a numeric type;

If it is a right-to-left operation, a++ first gets a numeric type! (a++) to obtain a Boolean type;

The logical non (!) and increment operator (+ +) are unary operators that, when the two unary operators are present in one line of code, operate from right to left, so that the result of a Boolean type is finally obtained;

x = a? B:c? D:e? F:g;

The trinocular operator is also right-associative, right-to-left arithmetic, equivalent to

x = a? B: (c. D: (E. f:g));

var a = b = c = D;

Even the assignment value, also from right to left operation, first take the value of D, assign it to C, and then assign the value of C to B, and then assign the value of B to A;

Order of Operations

What do you do when an expression contains an expression?

A = 1= + + + + A;console.log (A, b  ); // 2 3

This situation JS is always run from left to right;

    1. First JS will first calculate B, get B;
    2. Then the a++ is computed and the result is 1, but the value of a is changed to 2 after the process is finished.
    3. The third step calculates the back of a, gets 2;
    4. The fourth step calculates 1+2, obtains 3;
    5. Assign a value of 3 to B;

var a = 1= a + + + + A;  // 4
    1. First calculate a++, get 1, then a value becomes 2;
    2. Then calculate ++a, get 3;
    3. 1+3 get 4;
    4. Assign a value of 4 to B;

Unary Plus, unary minus, increment operator, decrement operator

These four operators are valid for all data types;

    • Unary Plus, unary minus is actually converting any data type into a numeric type (because all data types can be converted to numbers);
    • Increment operator, decrement operator converts any data type first to a number, and then adds one or minus one operation;

But when you decrement a decimal, the result may be different from what you think.

var a = 1.1; --a;  // 0.10000000000000009

Therefore, we must first convert the decimal number to an integer and then calculate the result and convert it back to decimals;

Bitwise non-

Just know a bit, a bitwise non-operation of a number, is to take a negative value minus one; For example, a bitwise non-operation of 10 results in 11;

Bitwise-AND

Can be used to judge the parity of numbers;

function even_or_odd (number) {    return (number & 1)? "ODD": "Even";}
    • The odd number is converted to binary and the last digit is 1, so the bitwise AND operation of 1 will return 1;
    • Even conversion to binary the last digit is 0, so the bitwise AND operation of 1 will return 0;

Logical Non-

Convert any data type to Boolean type, then reverse;

Unary operators in the form of characters

typeof: You can enclose the expression it wants to manipulate in parentheses, but don't mistake it as a function, the parentheses are just handy to let people see the arithmetic logic;

void: Returns a undefined no matter what the expression is;

Delete: The property used to delete the object;

Instanceof: Used to judge the constructor of an object, but it is not reliable and is not used much;

———— finishing from Shahi teacher's Front end Audio tutorial # accompany you to read books # (Himalaya)

A unary operator in JS

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.