JavaScript Logical Operators
JavaScript Operators
JavaScript Operators are used to assign values, compare values, perform arithmetic operations, and more.
Type | Common Use | Example |
---|---|---|
Comparison | Compares variables | (x == 5) |
Logical | Detefines the logic between variables | (x>0 || x>0) |
Bitwise | Does binary operations on numbers | (5 & 1) |
Javascript Comparison Operators
Comparison operators are used in logical statements to determine equality or difference between variables or values.
Given that x = 5, the table below explains the comparison operators:
Oper | Name | Comparing | Returns | Try it |
---|---|---|---|---|
== | equal to | x == 8 | false | Try it » |
== | equal to | x == 5 | true | Try it » |
=== | equal value and type | x === "5" | false | Try it » |
=== | equal value and type | x === 5 | true | Try it » |
!= | not equal | x != 8 | true | Try it » |
!== | not equal value or type | x !== "5" | true | Try it » |
!== | not equal value or type | x !== 5 | false | Try it » |
> | greater than | x > 8 | false | Try it » |
< | less than | x < 8 | true | Try it » |
>= | greater or equal to | x >= 8 | false | Try it » |
<= | less or equal to | x <= 8 | true | Try it » |
JavaScript Logical Operators
Logical operators are used to determine the logic between variables or values.
Given that x = 6 and y = 3, the table below explains the logical operators:
Oper | Name | Example | Try it |
---|---|---|---|
&& | AND | (x < 10 && y > 1) is true | Try it » |
|| | OR | (x === 5 || y === 5) is false | Try it » |
! | NOT | !(x === y) is true | Try it » |
JavaScript Bitwise Operators
Bit operators work on 32 bits numbers. Any numeric operand in the operation is converted into a 32 bit number. The result is converted back to a JavaScript number.
Oper | Name | Example | Same as | Result | Dec | Try it |
---|---|---|---|---|---|---|
& | AND | x = 5 & 1 | 0101 & 0001 | 0001 | 1 | Try it » |
| | OR | x = 5 | 1 | 0101 | 0001 | 0101 | 5 | Try it » |
~ | NOT | x = ~ 5 | ~0101 | 1010 | 10 | Try it » |
^ | XOR | x = 5 ^ 1 | 0101 ^ 0001 | 0100 | 4 | Try it » |
<< | Left shift | x = 5 << 1 | 0101 << 1 | 1010 | 10 | Try it » |
>> | Right shift | x = 5 >> 1 | 0101 >> 1 | 0010 | 2 | Try it » |
>>> | Unsigned right | x = 5 >>> 1 | 0101 >>> 1 | 0010 | 2 | Try it » |