6 Types of operators in JavaScript summary

Source: Internet
Author: User
Tags arithmetic operators logical operators

The JavaScript operators mainly include:

    1. Arithmetic operators
    2. Assignment operators
    3. Comparison operators
    4. Ternary operators
    5. logical operators
    6. String Join operators

operator Description Example result of Operation
+ Add y = 2+1 y = 3
- Reducing y = 2-1 y = 1
* By y = 2*3 y = 6
/ In addition, returns the result as a floating-point type y = 6/3 y = 2
% Remainder, returns the result as a floating-point type
Requires both operands to be integers
y = 6%4 y = 2
++ Sliding scale, divided into pre-plus and post-plus
The Boolean value and NULL will not be valid
y = 2
++y (ex Plus)
y++ (rear add)
y = 3
-- Descending, divided into former descending and after diminishing
The Boolean value and NULL will not be valid
y = 2
--y (pre-minus)
y--(post-subtraction)
y = 1

For the pre-plus and post-add, the result of the execution is the variable plus 1, the difference is that the execution of the return results are not the same, refer to the following two examples:

var x = 2;alert (++x); Output: 3alert (x); Output: 3var y = 2;alert (y++); Output: 2alert (y); Output: 3

Diminishing the same.

Assignment operators

Assignment operator = for assignment operations, the function of the assignment operator is to assign the right value to the left variable. Set y = 6, see the following table:

operator Example equivalent to result of Operation
= 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

Assignment Operations Nesting use

Assignment operators can be nested using:

y = (x = 2) + 5;    Results: x=2,y=7

Comparison operators

operator Description Example result of Operation
== Equals 2 = = 3 FALSE
=== Constant Equals (the value and type are to be compared) 2 = = = 2
2 = = = "2"
TRUE
FALSE
!= Not equal to, can also write <> 2 = = 3 TRUE
> Greater than 2 > 3 FALSE
< Less than 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 comparisons.

Ternary operators

Ternary can be seen as a special comparison operator:

(EXPR1)? (EXPR2): (EXPR3)

Syntax Explanation: When EXPR1 evaluates to TRUE, the value of the entire expression is expr2, otherwise EXPR3.

Example:

x = 2;y = (x = = 2)? X:1;alert (y); Output: 2

This example determines whether the value of X equals 2, and if X equals 2, then the value of y equals X (that is, 2), whereas Y equals 1.

Tips

In order to avoid errors, it is a good idea to enclose the three-operator expressions in parentheses.

logical operators

operator Description Example result of Operation
&& Logic and (and) x = 2;
y = 6;
X && y > 5
FALSE
|| Logical OR (OR) x = 2;
y = 6;
X && y > 5
TRUE
! Logic is not, take the opposite side of logic x = 2;
y = 6;
! (x > Y)
TRUE

String Join operators

The Join operator + is primarily used to concatenate two strings or string variables. Therefore, when you use the operator for a string or string variable, you do not do an addition calculation on them.

Example:

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

When you do a join (add) operation on strings and numbers, the numbers are first converted to strings and then concatenated (summed):

x = 25;y = "I am this year" + x + "year old"; Result: y = "I am 25 years old"


6 Types of operators in JavaScript summary

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.