An OPERATOR is a SYMBOL that represent an OPERATION to be taken on one or more VALUES
Operators can be represented by one or more characters.
e.g.
+
,-
,*
,||
,&&
Unary Operators
Binary Operators
Trinary Operators
+
The +
operator can be used on a single value to convert that value into a number.
+'33' // => 33
+'0' // => 0
+'' // => 0
+' ' // => 0
+'cheese' // => NaN
typeof
is a unary operator that returns the type of the operand
typeof function() {...} // => 'function'
typeof "Hello, world!" // => 'string'
typeof [1, 2, 3, "red"] // => 'object'
typeof unassignedVariable // => 'undefined'
typeof false // => 'boolean'
typeof null // => 'object'
Increment and decrement change their variable operand.
++
increment the variable by one--
decrement the variable by onelet myNumber = 3;
myNumber++
console.log(myNumber); // => 4
myNumber--
console.log(myNumber); // => 3
"cheese"++ // => NaN
NOTE: Increment and decrement only work on variables which have a Number value.
Accept two number values and perform an operation on them.
Returns a NEW number as a result.
+
Addition-
Subtraction*
Multiplication/
Division%
Modulus**
Exponent2 + 1 // => 3
2 - 1 // => 1
2 * 2 // => 4
6 / 2 // => 3
6 % 3 // => 0
5 ** 2 // => 25
Open a terminal and start node
, or create a new JavaScript file.
Determine answers to the following questions using arithmetic operators.
"12"
) to a number?NaN
?Evaluate two expressions and return a boolean
&&
and; returns true
if BOTH sides are considered truthy||
or; returns true
if EITHER side is considered truthy!
not; inverts the result of the preceding expression'dog' && 'cat' // => 'cat'
null && 'cat' // => false
'dog' || 'cat' // => 'dog'
undefined || 'cat' // => 'cat'
!true // => false
!(7 < 5) // => true
Compare two values and returns a boolean
>
greater than<
less than>=
greater than or equal to<=
less than or equal to5 > 3 // => true
5 < 3 // => false
5 >= 3 // => true
5 <= 5 // => true
==
equal to===
identical!=
not equal!==
not identical5 == '5' // => true
true == '1' // => true
5 === '5' // => false
true === '1' // => false
'cat' != 'dog' // => true
'cat' !== 'cat' // => false
There is a difference between the double equals ==
and triple equals ===
.
The double equals will try and coerce the operands so that are comparable.
The triple equals will only perform a comparison on the values as they are.
When comparing two values you should always use the triple equals.
Open up a node environment in your terminal, and let's play around with some comparison operators.
>
?
==
and triple equals ===
Modify an existing value by some other value.
=
sets the variable on the left equal to the value on the right+=
adds the value to the right to the variable on the left-=
subtracts the value to the right from the variable on the leftlet x = 7
let y = 3
x += 1 // x => 8
y += 3 // y => 6
x += y // x => 14
y -= x // x => -8
Open a terminal and start node
, or create a new JavaScript file.
x
using the let
keyword to a numerical value.y
using the const
keyword to a numerical value.+=
operator. What's the value of x
?+=
operator. Did you get the value you expected?string
that is a string.
string
using +=
, what is the value of string
?string
if you use -=
instead?The ternary operator is the only trinary operator in JavaScript
expression ? valueOne : valueTwo
// If the expression evaluates to true returns valueOne
// otherwise returns valueTwo
let isAllowed = true;
return isAllowed ? 'You are allowed' : 'You are denied';
// => 'You are allowed'
/