JavaScript Comparison and Logical Operators

The definition of an operator is a symbol that is used to perform an operation. Most often these operations are arithmetic (addition, subtraction, etc), but not always.

  • Assignment Operators
  • Comparison Operators
  • Arithmetic Operators
  • Bitwise Operators
  • Logical Operators
  • String Operators
JavaScript has the following types of operators. This section describes the operators and contains information about operator precedence.
operand1 operator operand2

For example, 3+4 or x*y. A unary operator requires a single operand, either before or after the operator:
operator operand Or operand operator For example, x++ or ++x.
Java Script has one ternary operator, the conditional operator. A ternary operator requires three operands.

Operator Precedence

Operator type Individual operators
member . []
call / create instance () new
negation/increment ! ~ – + ++ — typeof void delete
multiply/divide * / %
addition/subtraction + –
bitwise shift << >> >>>
relational < <= > >= in instanceof
equality == != === !==
bitwise-and &
bitwise-xor ^
bitwise-or |
logical-and &&
logical-or ||
conditional ?:
assignment = += -= *= /= %= <<= >>= >>>= &= ^= |=
comma ,

Assignment Operators

For example, x++ or ++x.An assignment operator assigns a value to its left operand based on the value of its right operand. The basic assignment operator is equal (=), which assigns the value of its right operand to its left operand. That is, x = y assigns the value of y to x.

The other assignment operators are shorthand for standard operations, as shown in the following table.

Shorthand operator Meaning
x += y x = x + y
x -= y x = x – y
x *= y x = x * y
x /= y x = x / y
x %= y x = x % y
x <<= y x = x << y
x >>= y x >>= y
x >>>= y x = x >>> y
x &= y x = x & y
x ^= y x = x ^ y
x |= y x = x | y

Comparison Operators

A comparison operator compare its operands and returns a logical value based on whether the comparison is true or not. The operands can be numerical, string, logical, or object values. Strings are compared based on standard lexicographical ordering, using Unicode values. If the two operands are not of the same type, JavaScript attempts to convert the operands to an appropriate type for the comparison, except for the === and !== operators. This generally results in a numerical comparison being performed. The following table describes the comparison operators.
Operator Description Example
== is equal to 5==8 returns false
=== is equal to (checks for both value and type) x=5y=”5″

x==y returns true

x===y returns false

!= is not equal 5!=8 returns true
> is greater than 5>8 returns false
< is less than 5<8 returns true
>= is greater than or equal to 5>=8 returns false
<= is less than or equal to 5<=8 returns true

Bitwise Operators

Bitwise operators treat their operands as a set of 32 bits (zeros(0) and ones(1)), rather than as decimal, hexadecimal, or octal numbers. For example, the decimal number nine has a binary representation of 1001. Bitwise operators perform their operations on such binary representations, but they return standard Java Script numerical values.
Operator Usage Description
Bitwise AND a & b Returns a one in each bit position for which the corresponding bits ofboth operands are ones.
Bitwise OR a | b Returns a one in each bit position for which the corresponding bits ofeither or both operands are ones.
Bitwise XOR a ^ b Returns a one in each bit position for which the corresponding bits ofeither but not both operands are ones.
Bitwise NOT ~ a Inverts the bits of its operand.
Left shift a << b Shifts a in binary representation b bits to left, shifting in zeros fromthe right.
Sign-propagating right shift a >> b Shifts a in binary representation b bits to right, discarding bitsshifted off.
Zero-fill right shift a >>> b Shifts a in binary representation b bits to the right, discarding bitsshifted off, and shifting in zeros from the left.

Logical Operators

Logical operators are typically used with Boolean (logical) values; when they are, they return a Boolean value. However, the && and || operators actually return the value of one of the specified operands, so if these operators are used with non-Boolean values, they may return a non-Boolean value. The logical operators are described in the following table.
Operator Description Example
&& and x=6y=3

(x < 10 && y > 1) returns true

|| or x=6y=3

(x==5 || y==5) returns false

! not x=6y=3

!(x==y) returns true


String Operator

In addition to the comparison operators, which can be used on string values, the concatenation operator (+) concatenates two string values together, returning another string that is the union of the two operand strings. For example, “my ” + “string” returns the string “my string”.The shorthand assignment operator += can also be used to concatenate strings. For example, if the variable mystring has the value “alpha,” thenthe expression mystring += “bet” evaluates to “alphabet” and assigns this value to mystring.
txt1=”What a very”txt2=”nice day!”

txt3=txt1+txt2

DOWNLOAD SOLUTION HERE