Result:
(a == b) => false
(a true
(a> b) => false
(a! = b) => true
(a> = b) => false
a <= b) => true
Đặt các biến vào các giá trị khác nhau, và tác tử khác khác và thử .
JavaScript supports the following logical operators:
Assuming variable A holds value 10 and variable B holds value 20, then:
STATE DEBT AND DESCRIPTION1&& (AND logic)
If both operands are non-zero, then the condition becomes true.
Ex: (A && B) is true.
2|| (Logical OR)
If 1 of the two operands is non-zero, then the condition becomes true.
Ex: (A || B) is true.
3! (NOT logic)
Reverse the state of the operand. If a condition is true, then the OR operator will cause it to be false.
Ex:! (A && B) is false.
For example
You try the following example showing how to use logical operators in JavaScript:
Set the variables to different values and different operators and then try.
Result:
(a && b) => false
(a || b) => true
! (a && b) => true
Đặt các biến vào các giá trị khác nhau, và tác tử khác khác và thử .
JavaScript supports the following bit manipulation operators:
Assuming variable A holds value 2 and variable B holds value 3, then:
STATE DEBT AND DESCRIPTION1& (Permission AND bit)
It performs an AND logic operation on each bit of its integer parameters.
Ex: (A & B) is 2.
2| (OR bit)
It performs a logical OR operation on each bit of its integer parameters.
Ex: (A | B) is 3.
3^ (XOR bit)
It performs a logical exclusion OR operation on each bit of its integer parameters. Permission or exclusion means that either operand 1 is true or operand 2 is true, but not both.
Ex: (A ^ B) is 1.
4~ (Bit negative)
It is a unary operator and reverses all bits in that operand.
Ex: (~ B) is -4.
5<< (Left translation)
It moves all the bits in the first operand to the left with the position number specified in the second operand. The new bits are filled by zero. Shifting a value to a position is equivalent to multiplying it by 2, translating the two positions equivalent to multiplying by 4, and so on.
Ex: (A << 1) is 4.
6>> (Right translation)
Operator must be binary. The value of the left operand is moved to the right by the number of bits specified by the right operand.
Ex: (A >> 1) is 1.
7>>> (Right translation with Zero)
This operator is quite similar to the >> operator, except that the bits shifted to the left are always zero.
Ex: (A >>> 1) is 1.
For example
Try the following code about the Bit operation operators in JavaScript:
Set the variables to different values and different operators and then try.
Result:
(a & b) => 2
(a | b) => 3
(a ^ b) => 1
(~ b) => -4
(a << b) => 16
(a >> b) => 0
Đặt các biến vào các giá trị khác nhau, và tác tử khác khác và thử .
JavaScript supports the following assignment operators:
STATE DEBT AND DESCRIPTION1= (Simple assignment)
Assign values from the right operand to the left operand
Ex: C = A + B will assign the value of A + B to C
2+ = (Addition and assignment)
It adds the right operand value to the left operand and assigns the result to the left operand
Ex: C + = A is equivalent to C = C + A
3- = (Subtraction and assignment)
It subtracts the right operand from the left operand and assigns the result to the left operand
Ex: C - = A is equivalent to C = C - A
4* = (Multiplication and assignment)
It multiplies the right operand with the left operand and assigns the result to the left operand
Ex: C * = A is equivalent to C = C * A
5/ = (Divide and assign)
It divides the left operand for the right operand and assigns the result to the left operand
Ex: C / = A is equivalent to C = C / A
6% = (Divide and assign division)
Divide the left operand balance for the right operand and assign the result as the remainder for the left operand
Ex: C% = A is equivalent to C = C% A
Note - Along with the logic development circuit for Bit operators, we will have the following operators: << =, >> =, >> =, & =, | = and ^ =.
For example
The following example is about using Bit operation operators in JavaScript:
Set the variables to different values and different operators and then try.
Result:
Value of a => (a = b) => 10
Value of a => (a + = b) => 20
Value of a => (a - = b) => 10
Value of a => (a * = b) => 100
Value of a => (a / = b) => 10
Value of a => (a% = b) => 0
Đặt các biến vào các giá trị khác nhau, và tác tử khác khác và thử .
Here, we will discuss two operators that are quite useful in JavaScript: Conditional operator (? :) and typeof operator .
The conditional operator first calculates an expression to see if it is true or false and then executes either of the provided commands depending on the result of the calculation.
? : (Condition): If the condition is true? Then the value X: If not then the value Y.
For example
Try the following code to understand how conditional operators work in JavaScript:
Set the variables to different values and different operators and then try.
Result:
((a> b)? 100: 200) => 200
((a 100
Đặt các biến vào các giá trị khác nhau, và tác tử khác khác và thử .
The typeof operator is a unary operator that is preceded by its single operand, which can be of any type. Its value is a string indicating the data type of the operand.
The typeof operator evaluates "number", "string", or "boolean" if its operand is numeric, string, or logical and returns either true or false on the estimate.
Below is a list of return values for the typeof operator.
Type string returned by typeofNumber "number" operator String "string" Boolean "boolean" Object "object" "function" Undefined "undefined" function Null "object"For example
The following code shows how to use the typeof operator.
Set the variables to different values and different operators and then try.
Result:
Result => B is String
Result => A is Numeric
Đặt các biến vào các giá trị khác nhau, và tác tử khác khác và thử .
According to Tutorialspoint
Previous article: Variable in JavaScript
Next lesson: if . else command in JavaScript