Home > Web Front-end > JS Tutorial > Summary of JavaScript operator usage

Summary of JavaScript operator usage

巴扎黑
Release: 2017-07-29 17:14:17
Original
1639 people have browsed it

This article mainly gives you a detailed summary of the operators in JavaScript, including common arithmetic operators, comparison operators and logical operators. It is very clear, friends who need it can refer to it.

In JavaScript, common operators include arithmetic operators, comparison operators and logical operators.

Table 1 JavaScript common operators

##--decrement. Decrement the operand by 1. n=--x;n=5 x=5##Comparison operator==##!=Not equal. If the two data are not equal, return true, otherwise return false. boolean2=(x!=5);boolean2=false;> is greater than. If the data on the left is greater than the data on the right, return true, otherwise return false. boolean4=(x>y);boolean4=true< is less than. If the data on the left is less than the data on the right, a Boolean value true is returned, otherwise false is returned. boolean5=(xboolean5=false>=Greater than or equal to. If the data on the left is greater than or equal to the data on the right, return true, otherwise return false. boolean6=(x>=y);boolean6=true<=Less than or equal to. If the data on the left is less than or equal to the data on the right, return true, otherwise return false. boolean7=(x<=y);boolean7=falseLogical operatorDescriptionExampleResult&&Logical AND. Returns true if the operands on both sides of the symbol are true, otherwise returns false. boolean_a=true&&false;boolean_a=false||Logical OR. If the operands on both sides of the symbol are false, return false, otherwise return true. boolean_b=true||false;boolean_b=true!Logical negation. Returns false if the operand to the right of the symbol is true, otherwise returns true. boolean_c=!true;boolean_c=false" + " sign can also be used to concatenate strings
Arithmetic operators Explanation Examples Result
= Assignment operator. Assign the value of the variable on the right side of the operator to the variable on the left. x = 5; -
+ plus sign. Add two numbers. y=1+2; y=3
- Minus sign. Subtract two numbers. z = x-y; z=2
* Multiply sign. Multiply two data together. a=x*y; a=15
/ Division sign. Divide two data. b=x/z; b=2.5
% Remainder operation. Find the remainder after dividing two numbers. c=x%z; c=1
++ Self-added. Add 1 to the operand. m=++x; m=6 x=6
Description Example Result
Equal. If the two data are equal, return true, otherwise return false. boolean1=(x==5); boolean1=true

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

For example:

The code is as follows:

 var name=" Tom ";
 var age=22;
 var person="My name is "+name+" ! I&#39;m  "+age+" ! ";
 alert(person);
Copy after login

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

In the above example , including strings and numerical values. When strings and numerical values ​​are mixed, JavaScript will automatically determine whether the "+" sign is used for addition or concatenation of strings. If concatenating strings, the numeric value will also be converted to a string.

Discussion on self-increasing (++) and self-decreasing (--)

It is worth noting that self-increasing (++) and self-decreasing (- -) The operator has different meanings when placed before and after the operand. If it is placed in front of the operand (front self-increment/front self-decrement), the operand will be added by 1 (subtracted by 1) first, and then the operation will be performed; if it is placed after the operand (last self-increment/back self-decrement), the operation will be performed first. Then add 1 to the operand (decrease 1).

For example:

The code is as follows:

 <script type="text/javascript">
 var x=5;
 var y=++x;  // 前自加,赋值后 x 的值为 6
 var z=x++;  // 后自加,赋值后 x 的值为 7
 var m=--x  // 前自减,赋值后 x 的值为 6
 var n=x--  // 后自减,赋值后 x 的值为 5
 </script>
 <p onclick="alert(y);">显示 y 的值</p>
 <p onclick="alert(z);">显示 z 的值</p>
 <p onclick="alert(m);">显示m 的值</p>
 <p onclick="alert(n);">显示 n 的值</p>
Copy after login

Save and run the code, click on the four pieces of text in sequence, and all of them will display 6.

Analysis:

For y, the value of x (x=5) plus 1 becomes 6, and then the value of x is passed to y.

For z, first pass the value of x (x=6) to z, then add 1 to x, and the value becomes 7.

For m, the value of x (x=7) minus 1 is 6, and then the value of x is passed to m.

For n, first pass the value of x (x=6) to n, then subtract 1 from x, and the value becomes 5.



Abbreviations of arithmetic operators

In order to facilitate operation and reduce code writing, JavaScript also supports abbreviations of common mathematical operators.

Table 2 Abbreviations of common arithmetic operators

OperatorExample is equivalent to +=x+=yx=x+y-=x-=yx=x-y*=x*=yx=x*y/=x/=yx=x/y%=x%=yx=x%y

The above is the entire content of this article, I hope you all like it.


The above is the detailed content of Summary of JavaScript operator usage. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template