JavaScript operator collation

Source: Internet
Author: User
Tags bitwise bitwise operators

Speaking of operators, basically all kinds of programming languages are involved, the use of a similar way. Here is a simple collation of JavaScript here today.

In general, operators are more than the comparison, can be divided into the following types:

    • Unary operators
    • Bitwise operators
    • Boolean operator
    • Multiplicative operators
    • Additive operators
    • Relational operators
    • Equal-sex operators
    • Conditional operators
    • Assignment operators
    • Comma operator

It can be said that as long as the learning to become a language on these operators should be not unfamiliar, as long as there is logic or calculation will use the above operator, I have been relatively fragmented before, and never systematically learn and organize how to use these operators? What are the differences and connections between them? What is the use of the tips? Today, we will study and explain the issues mentioned above.

Unary operators

Delete operator, remove references to previously defined object properties or methods, return False when Delete fails, and when will delete fail? The first cannot delete the declared variables (variables, functions, function parameters), such as: var a = 1; the second cannot delete the method of the built-in object, such as: Math.PI; The configurable value of the variable property of the third definition is false and cannot be deleted. It is important to note that under Ie6-8, the Delete window property will report a type error. Specifically, you can learn about the JavaScript delete mechanism that was written earlier.

The void operator, which returns undefine for any value, is commonly used to avoid values that should not be output by the output. Our common is <a href= "javascript:void ();" > Point me </a> so this link has no effect, because it returns a undefine, does not execute the default event of the a tag, it is equivalent to executing a return false.

The former increment/decrement operator refers to the value of the variable itself increased by 1 or minus 1. The post-increment/post-decrement operator refers to the value of the variable itself being incremented by 1 or minus 1. The difference is that if you assign this operation to a value, there is a difference:

var fnum = 1;var Bnum = 1;var forward = ++fnum; Fnum = 2; Forward = 2;var back = bnum++; Bnum = 2; back = 1//decrement operator The result is similar

unary addition and unary subtraction operators, unary addition and subtraction are the same for numbers, which is mathematical addition and subtraction, but the unary addition has a magical effect in the string. Related to JavaScript data type conversion, interested students can also learn about the one or two.

Bitwise operators

Before you learn bit operators, you need to know that the bitwise operators operate at the bottom of the number (that is, the 32 digits that represent numbers). ECMAScript integers are divided into two types: signed integers (allow positive and negative numbers) and unsigned integers (only positive numbers are allowed). In ECMAScript, signed integers are used by default. This means that there are 31 digits for the value, one for the symbol, 0 for negative, and 1 for positive. Negative numbers are also stored as binary code, but in the form of twos complement. So how do you calculate the twos complement? First find out the absolute value of the binary code is also called the original code, and then find the original code of the anti-code, and finally the anti-code plus 1 is the complement. Interestingly, ECMAScript is not displayed in the form of twos complement, but instead displays the absolute value of the binary code with the minus sign before the binary.

var num1 = 18;var num2 = -18;num1.tostring (2); ' 10010 ' num 2.toString (2); '-10010 '

  

Understanding this foundation, you can learn the bit operators in JS.

The bitwise operator not, which is represented by the ~, is calculated by first turning the operand into a 32-digit number, then converting it to its inverse code, and finally turning the binary into a floating-point. In fact, this process is still very difficult to convert, bit operation is not essentially, is to negative value and then minus 1 (here is said to be calculated by the whole number).

Bitwise operations and, which are represented by the &, convert the values into binary, then bitwise AND, and operate as follows:

Digits in the first digit

Digits in the second number

Results

1

1

1

1

0

0

0

1

0

0

0

0

bitwise operations OR, which are represented by |, convert the values into binary, then bitwise OR, and the operation rules are as follows:

Digits in the first digit

Digits in the second number

Results

1

1

1

1

0

1

0

1

1

0

0

0

Bitwise operation XOR, which is represented by ^, converts the values into binary, then bitwise XOR, and the operation rules are as follows:

Digits in the first digit

Digits in the second number

Results

1

1

0

1

0

1

0

1

1

0

0

0

Left-shift operation, is two less than (<<). It shifts all digits in the number to the left by the specified number. After moving, make 0 on the right and convert to decimal. Such as:

var num = 2; Binary: ' Ten ' var newnum = num << 5;console.log (newnum); 642 binary: ' 1000000 '

A signed right-shift operator, represented by two greater-than sign (>>). It shifts all the digits in the number to the right by the specified number, preserving the sign bit. After moving, make 0 on the left and then convert to decimal. Such as:

var num = 64; Binary: ' 1000000 ' var newnum = num >> 5;console.log (newnum); 22 binary: ' 10 '

The unsigned right-shift operator, represented by three greater-than sign (>>>). The difference between this and the signed right shift is that the unsigned need to move the whole, so that a negative number becomes a positive number. So be careful when you use it.

Boolean operator

Before you learn, you need to know how other data types are converted to Boolean types, as follows:

Parameter type

Results

Undefine

False

Null

False

Boolean

Do not convert

Number

0 is False,nan is false, others are true

String

Null string is False, other is true

Object

True

The logical not operator, using the! representation. is a process of non-seeking.

The logical AND operator, using &&, shows that only two of the expressions on both sides are true, and the result is true, and all other cases are false.

Logical OR operator, using | | Indicates that only two of the expressions on both sides are false, and the result is false, and all other conditions are true.

It should be noted that if && and | | If they are not used in conditional statements, their operations are left-to-right. Such as:

1 && 2//1 is true, continue to calculate, 2 is also true, so the result is && 1//0 is false, so it does not calculate to the right, the result is 01 | | 2//1 is true, so the result is 10 | | 1//0 is false, continuation is calculated, 1 is true, so the result is 1

Multiplicative operators

The multiplication operator, denoted by the * number, is the multiplication operation in mathematics.

The division operator, denoted by A/number, is the division operation in Mathematics.

The modulo operator, denoted by the% number, divides the calculus in mathematics.

Additive operators

Normal addition and subtraction calculations.

Relational operators

The normal comparison method, the value is greater than, less than, greater than or equal to, less than equals.

Compare numbers and strings, if all are numeric strings, then convert to numbers and compare size, if a number is a string or two is a string, compare the order of the character code.

Equal-sex operators

Equal and non-equal, the expression that requires both sides of the operator should be data-type-cast. The rules are as follows:

The rules for performing type conversions are as follows:

If one of the operands is a Boolean value, convert it to a numeric value before checking for equality. False converts to 0,true to 1.

If one operand is a string and the other is a number, try converting the string to a number before checking for equality.

If one operand is an object and the other is a string, try to convert the object to a string before checking for equality.

If one operand is an object and the other is a number, try converting the object to a number before checking for equality.

The operator also adheres to the following rules when comparing:

The value null and undefined are equal.

When checking for equality, null and undefined cannot be converted to other values.

If an operand is NaN, the equal sign returns false, and the non-equal sign returns TRUE.

If the two operands are objects, then their reference values are compared. If two operands point to the same object, then the equals sign returns TRUE, otherwise two operands are not equal.

Important: Even though two numbers are Nan, the equals sign still returns false because Nan is not equal to Nan, according to the rule.

Full Equals and non-full equals, which do not perform data type conversions at the time of operation, judging the type before judging the value.

Conditional operators

Also known as the trinocular operator, three expressions, only one expression is true, the result is the value of the second expression, the first expression is false, and the result is the value of the third expression.

Assignment operators

We define variables that are most commonly used, using the = sign. In addition, the main arithmetic operators can be used in conjunction with the assignment operator. such as: *=, + = and so on.

Comma operator

A comma operator that can perform multiple operations in a single statement. The comma operator is commonly used in variable declarations. Such as:

var a = 1, b = 2, c = 3; A=1;b=2;c=3var a = n/A; A=3, last value var d = (a=1,b=2,c=3); What is the value of d?

There is less contact than the main bit operators, in fact, bit operators in the privilege handling is particularly convenient.

JavaScript operator collation

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.