JavaScript Operators Summary _javascript tips

Source: Internet
Author: User
Tags arithmetic arithmetic operators logical operators numeric value

In JavaScript, the common operators are the arithmetic, comparison, and logic operators.

Table 1 JavaScript Common operators

Arithmetic Operators Description examples Results
= The assignment operator. Assigns the value of the right-hand variable of the operator to the left variable. x = 5; -
+ Plus. Adds two data. y=1+2; Y=3
- Minus sign. Subtracts two data. z = x-y; z=2
* Multiplication Multiplies two data. A=x*y; A=15
/ Division Divides two data. b=x/z; b=2.5
% The remainder operation. Find the remainder of the two data division. C=x%z; C=1
++ Self added. Add an operand of 1. M=++x; M=6 x=6
-- Self reduction. Reduce the number of operands by 1. N=--x; N=5 x=5
comparison Operators Description examples Results
== Equal. Returns true if the two data are equal, or false. Boolean1= (x==5); Boolean1=true
!= Not equal. Returns true if the two data are not equal, or false. Boolean2= (x!=5); Boolean2=false;
> Greater than. Returns true if the left data is greater than the right data, otherwise returns false. Boolean4= (X>y); Boolean4=true
< Less than. Returns a Boolean value of True if the left data is less than the right data, otherwise returns false. Boolean5= (X<y); Boolean5=false
>= is greater than or equal to. Returns true if the left data is greater than or equal to the right data, otherwise returns false. Boolean6= (x>=y); Boolean6=true
<= is less than or equal to. Returns true if the left data is less than or equal to the right data, otherwise returns false. Boolean7= (x<=y); Boolean7=false
logical Operators Description examples Results
&& Logic and. Returns true if the operands on both sides of the symbol are true, or false. boolean_a=true&&false; Boolean_a=false
|| Logical OR. Returns False if the operands on both sides of the symbol are false, otherwise true. boolean_b=true| | False Boolean_b=true
! Logic is not. Returns False if the operand to the right of the symbol is true, otherwise returns true. Boolean_c=!true; Boolean_c=false

The "+" number can also be used to connect strings

The "+" number can be used not only to add two data, but also to connect strings.

For example:

Copy Code code as follows:

var name= "Tom";
var age=22;
var person= ' My name is ' +name+ '! I ' m ' +age+ '! ";
alert (person);

Save and run the code to display my name is Tom! I ' m 22!

In the example above, there are strings and numbers. When a string is mixed with a numeric value, JavaScript automatically determines whether the "+" number works, whether it is a summation operation or a connection string. If this is a connection string, the numeric value is also converted to a string.

Discussion on self-addition (+ +) and self-reduction (-)

It is noteworthy that the self-addition (+ +) and decrement (-) operators are placed before and after the operands in a different meaning. Put in front of operand (before plus/before self reduction), the operand is added 1 (minus 1), then the operation is placed after the operand (after the addition/after the self-subtraction), the first operation, and then the operand plus 1 (minus 1).

For example:

Copy Code code as follows:

<script type= "Text/javascript" >
var x=5;
var y=++x; The value of x after the assignment is 6
var z=x++; The value of x after the assignment is 7
var m=--x//Ex-minus, value x after assignment is 6
var n=x--//After self reduction, value of X after assignment is 5
</script>
<p onclick= "alert (y);" > Show Y's value </p>
<p onclick= "alert (z);" > Show Z's value </p>
<p onclick= "alert (m);" > Show M's Value </p>
<p onclick= "alert (n);" > shows the value of n </p>

Save and run the code, click Four paragraphs in turn, and show 6.

Analysis:

For y, X (x=5) plus 1, the value becomes 6, and then the value of x is passed to Y.
For z, the value of x (x=6) is passed to Z first, then x plus 1, and the value becomes 7.
For M, X (x=7) minus 1, the value is 6, and then the value of x is passed to M.
For N, first pass the value of X (x=6) to N, minus 1 to X, and the value to 5.

Abbreviation for arithmetic operator

JavaScript also supports abbreviations for common mathematical operators for easy operation and less code writing.

Table 2 abbreviations for common arithmetic operators


operator Example equivalent to
+= X+=y X=x+y
-= X-=y X=x-y
*= X*=y X=x*y
/= X/=y x=x/y
%= X%=y X=x%y

The above mentioned is the entire content of this article, I hope you can enjoy.

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.