Home >Web Front-end >Front-end Q&A >What are the javascript operators?

What are the javascript operators?

青灯夜游
青灯夜游Original
2022-02-21 17:24:252001browse

Operators include: 1. Arithmetic operators, including " ", "-", "*", etc.; 2. Assignment operators, including "=", "=", "-=", etc.; 3. , Comparison operators, including "==", "===", etc.; 4. Logical operators, including "&&", "||", etc.; 5. Bit operators, including "&", "|", etc. .

What are the javascript operators?

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

JavaScript operator (operator)

In JavaScript, operators are used to assign values, compare values, perform arithmetic operations, etc.

Arithmetic operators

Arithmetic operators are used to perform operations on two variables or values.

Assign y = 5, the following table will explain to you the use of arithmetic operators:

##%remainder x = y % 2y = 5x = 1incrementx = yy = 6x = 6x = y y = 6#x = 5--decrementx = --yy = 4x = 4x = y--y = 4x = 5
Operator Description Example y value x value
Addition x = y 2 y = 5 #x = 7
- Subtraction x = y - 2 y = 5 #x = 3
* Multiplication x = y * 2 y = 5 #x = 10
/ Division x = y / 2 y = 5 x = 2.5

Assignment operator

The assignment operator is used to assign values ​​to JavaScript variables.

Given x=10 and y=5, the following table explains the assignment operators:

OperatorExampleInstancex Value=x = yx = yx = 5 =x = yx = x y#x = 15-=x -= yx = x - yx = 5 *=x *= yx = x * yx = 50/=x /= yx = x / yx = 2%=x %= y x = x % yx = 0
##String operators

operator, = operator can be used to concatenate strings.

Given text1 = "Good ", text2 = "Morning", and text3 = "", the following table explains the use of string operators:

Operator =
Example text1 #text2 text3
text3 = text1 text2 "Good " "Morning" "Good Morning"
text1 = text2 "Good Morning" "Morning" ""
Comparison operators

Comparison operators are used to judge logical statements to determine whether two given values ​​or variables are equal.

Given x=5, the following table shows the use of comparison operators:

Operator==x == 5===x === 5!=!==x ! == 5##> is greater than x > 8false< is less than x < 8truegreater than or equal toless than Or equal to true
Description Comparison The result
is equal to x == 8 false
true
The value and type are equal (constantly equal to ) x === "5" false
true
is not equal to x != 8 true
The value and type are not equal (not equal) x !== "5" true
false
##> =
x>= 8 false <=
x <= 8

Conditional operator

The conditional operator is used for assignment operations based on conditions.

Given x=6 and y=3, the following table demonstrates the operation of the conditional operator:

SyntaxExample
Variable= (Condition) ? Value 1:Value 2voteable = (age < 18) ? "Too young to": "Age enough";

Logical operator

Logical operators are used to determine the logical relationship between variables or values.

Given x=6 and y=3, the following example demonstrates the use of logical operators:

OperatorDescription Example
&& and (x < 10 && y > 1) are true
|| or (x == 5 || y == 5) is false
! Not !(x == y) is true

##Bitwise operator

Bit operators work on 32-bit numbers. Any numeric operations will be converted to 32 bits. The result is converted to a JavaScript number.

OperatorDescriptionExampleSimilar toResultdecimal&ANDx = 5 & 10101 & 00010001 1|ORx = 5 | 10101 | 00010101 5~ Negation#x = ~ 5 ~0101 1010 -6^XORx = 5 ^ 10101 ^ 00010100 4##<<##>> Shift rightx = 5 >> 10101 >> 100102[Related recommendations: javascript learning tutorial
Move left x = 5 << 10101 << 11010 10

]

The above is the detailed content of What are the javascript operators?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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