JavaScript operator collation

Source: Internet
Author: User
Tags arithmetic operators object expression numeric value variables string variable tostring

Speaking of operators, basically all kinds of programming languages will be involved, the use of similar methods. Here's a simple collation of JavaScript here today.

In general, operators are still relatively much more, can be divided into the following types:

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

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

Unary operator

Delete operator, deletes a reference to an object property or method that was previously defined, returns False when the deletion fails, and when does the deletion fail? The first cannot delete declared variables (variables, functions, function arguments), such as: var a = 1; second, methods that cannot delete the built-in objects, such as: Math.PI; The configurable value of the third-defined variable property is false and cannot be deleted. It should be noted that under Ie6-8, the Delete window property reports a type error. Specifically, you can learn about the delete mechanism of JavaScript that you wrote before

The void operator, which returns undefine for any value, which is often used to avoid values that the output should not output. Our common is <a href= "javascript:void ();" > Point I </a> this link will have no effect, because the return of a undefine, do not perform a label of the default event, the equivalent of the execution of returns false.

The forward increment/front decrement operator, which refers to the value of the variable itself by 1 or minus 1. The post increment/post decrement operator, which refers to the value of the variable itself by 1 or minus 1. The difference is that if you assign a value to this operation, there is a difference:

var fnum = 1;   var   bnum = 1;   var forward = ++fnum; //fnum = 2; forward = 2;   var back = bnum++; //bnum = 2; back = 1 //减量运算符结果是类似的

unary addition and unary subtraction operators, unary addition and subtraction are the same for numbers, mathematical addition and subtraction operations, but one-element addition in the string has a magical effect. The data types involved in JavaScript conversion, interested students can also learn about one or two here.

Bitwise operators

Before learning the bitwise operator, you need to know that the bitwise operator operates at the bottom of the number (that is, the 32 digits of the number). ECMAScript integers are divided into two types: signed integers (allowing 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 representing the numeric value, one representing the symbol, where 0 is negative and 1 is a positive number. Negative numbers are also stored as binary code, but the form of binary complement is used. Then how to calculate the binary complement? First, the digital absolute value of the binary code is also called the original code, and then find the original code of the inverse code, and finally the counter Code plus 1 is the complement. Interestingly, ECMAScript is not displayed as a binary complement, but rather a binary code that displays the absolute value of the value before the minus sign.

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 ~, 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 transform, the bit is not in essence, is to the value of the negative and then minus 1 (here is said to be calculated by integer digits).

Bitwise operation and, which is represented by &, converts values into binary, and then bitwise and operations, with the following rules:

Digits in the first digit

Number of digits in the second digit

Results

1

1

1

1

0

0

0

1

0

0

0

0

bitwise OR, which is represented by changing the numeric value to binary, and then by bitwise OR, the operation rules are as follows:

Digits in the first digit

Number of digits in the second digit

Results

1

1

1

1

0

1

0

1

1

0

0

0

Bitwise operation XOR, which is represented by ^, converts numeric values into binary, and then bitwise XOR operations, with the following rules:

Digits in the first digit

Number of digits in the second digit

Results

1

1

0

1

0

1

0

1

1

0

0

0

The left shift operation is a two less than (<<) number. It moves all digits in the number to the left by a specified number. After the move, the right 0, and then converted to decimal. Such as:

var num = 2; // 二进制为:’10' var newNum = num << 5;   console.log(newNum); // 64 二进制为:’1000000’

The signed right shift operator, represented by two greater-than sign (>>). It shifts all digits in a number to the right by the specified number, preserving the symbol bit. After the move, the left 0, and then converted to decimal. Such as:

var num = 64; // 二进制为:’1000000’ var newNum = num >> 5;   console.log(newNum); //2 二进制为:’10’

The unsigned Right shift operator, represented by three greater-than sign (>>>). The difference between this and a 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 operators

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, others are true

Object

True

Logical NOT operator, using! is a process of seeking for the non.

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

The logical OR operator, used to indicate that only two of the expressions on both sides are false, the result is false, and the other is true.

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

1 && 2 //1为true,继续计算,2也为true,所以结果是2   0 && 1//0为false,所以就不会向右计算了,结果就是0 1 2 //1为true,所以结果是1   0 1 //0为false,继续计算,1为true,所以结果为1

Multiplicative operator

Multiplication operator, which is represented by the *, multiplication operation in mathematics.

The division operator, represented by a/number, is a division operation in mathematics.

The modulo operator, expressed in%, is rounded by the division operation in Mathematics.

Additive operator

Normal addition and subtraction calculations.

Relational operators

The general comparison method, with values greater than, less than, greater than or equal to, or less than or equal to.

Compare numbers and strings, if they are numeric strings, then convert to numbers and then size, and compare the sequence of character codes if a number is a string or two are strings.

The Equal sex operator

An equal sign and a non-equal sign, which requires an expression on both sides of the operator to be data type conversion. The rules are as follows:

The rules for performing type conversions are as follows:

If an operand is a Boolean value, convert it to a numeric value before checking for equality. False converts to 0,true 1.

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

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

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

When compared, the operator also adheres to the following rules:

Value null and undefined equal.

You cannot convert null and undefined to other values when checking for equality.

If one of the operands is NaN, the equal sign returns false, and a non-equal sign returns TRUE.

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

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

The full equals sign and the non full equals sign, this is not the data type conversion in the operation, first determines the type to judge the value.

Conditional operator

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

Assignment operator

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

Comma operator

The comma operator, in which you can perform multiple operations in a single statement. Comma operators are often used in variable declarations. Such as:

var a = 1, b = 2, c = 3; // a=1;b=2;c=3   var a = 1,2,3; // a=3 ,最后一个值 var d = (a=1,b=2,c=3); //d的值是什么呢?

Everyone touches less mainly bitwise operators, in fact bitwise operators are particularly handy in the handling of permissions



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.