Summary of six operators in JavaScript, javascript Operators

Source: Internet
Author: User

Summary of six operators in JavaScript, javascript Operators

JavaScript operators include:

  1. Arithmetic Operators
  2. Value assignment operator
  3. Comparison Operators
  4. Ternary Operators
  5. Logical operators
  6. String concatenation operator

Arithmetic Operators

Operator Description Example Calculation Result
+ Add Y = 2 + 1 Y = 3
- Subtraction Y = 2-1 Y = 1
* Multiplication Y = 2*3 Y = 6
/ Besides, the returned result is of the floating point type. Y = 6/3 Y = 2
% Return result is of the floating point type.
Both operands must be integers.
Y = 6% 4 Y = 2
++ Increment, divided into prefix and postincrement
The values of Boolean and NULL are invalid.
Y = 2
++ Y (prefix)
Y ++ (add later)
Y = 3
-- Descending, divided into descending and descending
The values of Boolean and NULL are invalid.
Y = 2
-- Y (minus)
Y -- (minus)
Y = 1

For prefix and Postfix, the result after execution is a variable plus 1. The difference is that the returned results are different during execution. See the following two examples:

Copy codeThe Code is as follows:
Var x = 2;
Alert (++ x); // output: 3
Alert (x); // output: 3

Var y = 2;
Alert (y ++); // output: 2
Alert (y); // output: 3

The same is true for decline.

Value assignment operator

The value assignment operator = is used for the value assignment operation. The value assignment operator assigns the value on the right to the variable on the left. Set y = 6. See the following table:

Operator Example Equivalent Calculation Result
= Y = 6 �� Y = 6
+ = Y + = 1 Y = y + 1 Y = 7
-= Y-= 1 Y = Y-1 Y = 5
* = Y * = 2 Y = y * 2 Y = 12
/= Y/= 2 Y = y/2 Y = 3
% = Y % = 4 Y = y % 4 Y = 2
Nested value assignment

Value assignment operators can be nested:

Copy codeThe Code is as follows:
Y = (x = 2) + 5; // result: x = 2, y = 7

Comparison Operators

Operator Description Example Calculation Result
= Equal 2 = 3 FALSE
=== Constant equals (compare values and types) 2 = 2
2 = "2"
TRUE
FALSE
! = Not equal to or writeable <> 2 = 3 TRUE
> Greater 2> 3 FALSE
< Less 2 <3 TRUE
> = Greater than or equal 2> = 3 FALSE
<= Less than or equal 2 <= 3 TRUE

Comparison operators can also be used for string comparison.

Ternary Operators

Three elements can be considered as special comparison operators:

Copy codeThe Code is as follows:
(Expr1 )? (Expr2): (expr3)

Syntax explanation: when the value of expr1 is TRUE, the value of the entire expression is expr2; otherwise, it is expr3.

Example:

Copy codeThe Code is as follows:
X = 2;
Y = (x = 2 )? X: 1;
Alert (y); // output: 2

In this example, we determine whether the value of x is equal to 2. If x is equal to 2, the value of y is equal to x (that is, 2), and y is equal to 1.

Prompt

To avoid errors, it is a good idea to enclose the expressions of the ternary operators in parentheses.

Logical operators

Operator Description Example Calculation Result
&& Logic and (and) X = 2;
Y = 6;
X & y> 5
FALSE
| Logic or (or) X = 2;
Y = 6;
X & y> 5
TRUE
! Non-logical, taking the opposite of Logic X = 2;
Y = 6;
! (X> y)
TRUE

String concatenation operator

The concatenation operator + is used to connect two strings or string variables. Therefore, when this operator is used for strings or string variables, it is not used for addition calculation.

Example:

Copy codeThe Code is as follows:
X = "beijing ";
Y = x + "Hello! "; // Result: y =" beijing hello! "

// To add a space between two strings, insert the space into a string:
Y = x + "Hello! "; // Result: y =" beijing hello! "

When you connect a string to a number (addition), the number is first converted to a string and then connected (added ):

Copy codeThe Code is as follows:
X = 25;
Y = "I am" + x + "years old"; // result: y = "I am 25 years old"


Javascript Operators

Upstairs is ....
Var url = "/abc/ddx/index.html"; var file = url. substring (url. lastIndexOf ('/') + 1); alert (file );


Javascript bitwise Operator

| Yes or operator
The basic principle of the or operation is: if the bits corresponding to two numbers are 1 or one of them is 1, the result is 1. If both are 0, the result is 0.
For example:
For example
1000 for everyone in 8
Then 4 | 8 is 1100
To convert to a 10-digit system, the value is 12.
It can be understood in plain terms, or operations can overlay the two states, for example
0100 stands for brave
1000 represents wit
The result of the OR operation 1100 is brave and witty.
If 1100 and 0100 are used for the OR operation, the result is still 1100, because 1100 already contains the status 0100.
Do you understand?

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.