Conditional (ternary) operator
= x = y 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
The + operator can also be used to add (concatenate) strings.
Adding Strings and Numbers
Adding two numbers, will return the sum, but
adding a number and a string will return a string:
x = 5 + 5;
y = "5" + 5;
z = "Hello" + 5;
The result of x, y, and z will be:
== equal to
=== equal value and equal type
!= not equal
!== not equal value or not equal type
> greater than
< less than
>= greater than or equal to
<= less than or equal to
? ternary operator
typeof Returns the type of a variable
instanceof Returns true if an object is an instance of an object type
!= Is not equal to
> Greater Than
< Less Than
>= Greater Than or equal to
<= Less Than or equal to
=== Is exactly like and is of the same variable type as
Binary Logical Operators
That is, if the first operand suffices for determining the result,
the second operand is not evaluated. For example, in the following expressions, the function foo() is never called:
false && foo()
true || foo()
If the first operand is falsy, return it. Otherwise, return the second operand.
If the first operand is truthy, return it. Otherwise, return the second operand.
Normal, or lenient, (in)equality: == and !=
Strict (in)equality: === and !==
Operators you will want to use with your IF Statements are the logical ones.
The logical operators give you more options for your IF statements.
&& Two ampersands mean AND
|| Two pipe characters mean OR
! One exclamation mark/point means NOT
There are also some operations that can be applied to Boolean values themselves.
The && operator represents logical and. It is a binary operator,
and its result is true only if both the values given to it are true.
console.log(true && false)
console.log(true && true)
The || operator denotes logical or. It produces true if either of the values given to it is true.
console.log(false || true)
console.log(false || false)
Not is written as an exclamation mark (!).
It is a unary operator that flips the value given to it-!true produces false and !false gives true.
The Ternary Operators
If you want a quick IF statement then you can use something called a ternary expression. This uses a question mark ( ? ) and a colon ( : ). The syntax is this:
var result = condition_to_test ? "TRUE" : "FALSE";
Here's a coding example:
var over_eighteen = true;
var result = over_eighteen ? "Truth" : "False";