Home  >  Article  >  Web Front-end  >  JavaScript Enhancement Tutorial - JavaScript Operators

JavaScript Enhancement Tutorial - JavaScript Operators

黄舟
黄舟Original
2017-01-21 15:54:311547browse

This article is the official HTML5 training tutorial of the H5EDU organization. It mainly introduces: JavaScript intensive tutorial - JavaScript operators
JavaScript 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:

Operator description example y value x value online example
+ addition x = y + 2 y = 5 x = 7 Example
- Subtraction x = y - 2 y = 5 x = 3 Example
* Multiplication x = y * 2 y = 5 x = 10 Example
/ Division x = y / 2 y = 5 x = 2.5 Example
% Remainder x = y % 2 y = 5 x = 1 Example
++ Increment x = ++y y = 6 x = 6 Example
x = y++ y = 6 x = 5 Example
-- Decrement x = --y y = 4 x = 4 Example
x = y-- y = 4 x = 5 Example

Regarding arithmetic operators, you You can read our JavaScript operators tutorial.

JavaScript 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 operator:

Operator Example Same As x Value Online Example
= x = y x = y x = 5 Examples
+= x += y x = x + y x = 15 Examples
-= x -= y x = x - y x = 5 Examples
*= x *= y x = x * y x = 50 Examples
/= x /= y x = x / y x = 2 Example
%= x %= y x = x % y x = 0 Example

For assignment operators, you can read our JavaScript operators Talisman tutorial.

JavaScript 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 examples text1 text2 text3 Online example
+ text3 = text1 + text2 "Good " "Morning" "Good Morning" Example
+= text1 += text2 "Good Morning" "Morning" "" Example
Comparison operator

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 Description Comparison Result Online Example
== Equal to x == 8 false Example
x == 5 true instance
=== Values ​​and types are equal (constantly equal) x === "5" false instance
x === 5 true instance
!= is not equal to x != 8 true instance
!== The value and type are not equal (not equal) x !== "5" true instance
x !== 5 false instance
> greater than x > 8 false Example
< less than x < 8 true Example
>= greater than or equal to x >= 8 false Example
<= less than or equal to x <= 8 true Example

For comparison operators, you can read our JavaScript comparison operators tutorial.

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:

Syntax example online example
Variable = (condition) ? value 1: value 2 voteable = (age & 18) ? "Too young" : "Old enough" Example
Logical operators

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:

Operator Description Example
&& and (x < 10 && y > 1) is true
|| or (x == 5 || y == 5) is false
! Not! (x == y) is true
JavaScript Bit 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.
Operator description example is similar to the result decimal
& AND x = 5 & 1 0101 & 0001 0001 1
| OR x = 5 | 1 0101 | 0001 0101 5
~ Negate x = ~ 5 ~0101 1010 10
^ XOR x = 5 ^ 1 0101 ^ 0001 0100 4
<< Shift left/td> ##>> Right shift Chinese website (www.php.cn)!

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