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

JavaScript Enhancement Tutorial - JavaScript Operators

巴扎黑
巴扎黑Original
2016-11-25 15:18:54939browse

This article is the official HTML5 training tutorial of 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»

For arithmetic operators, 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 operators:

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

For assignment operators, you can read our JavaScript operators 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 Example text1 text2 text3 Online Example
+ text3 = text1 + text2 "Good " "Morning" "Good Morning" Example »
+= text1 += text2 "Good Morning" "Morning" "" Example »
Comparison operator

Comparison operator is used to judge logical statements to determine the given Whether two specified 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 Example »
=== Values ​​and types are equal (equal to) x === "5" false Example »
x === 5 true Example »
!= Not equal to x != 8 true Example »
!== Values ​​and types are not equal (not identical) x !== "5" true instance »
x !== 5 false instance »
> greater than x > 8 false instance »
< less than x < 8 true instance »
>= 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

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) are true
|| or (x == 5 || y == 5) is false
! Not!(x == y) is true
JavaScript Bit Operators

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 Or x = 5 ^ 1 0101 ^ 0001 0100 4
<< Shift left/td> ; 1 0101 >> 1 0010 2

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