What is an Operator?

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. +, -, *, ||, &&


Types of Operators


The Unary +

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

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

Increment and decrement change their variable operand.

let 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.


Arithmetic Operators

Accept two number values and perform an operation on them.

Returns a NEW number as a result.

2 + 1  // => 3
2 - 1  // => 1
2 * 2  // => 4
6 / 2  // => 3
6 % 3  // => 0
5 ** 2 // => 25

Lab: Arithmetic

Open a terminal and start node, or create a new JavaScript file.

Determine answers to the following questions using arithmetic operators.


Logical Operators

Evaluate two expressions and return a boolean

'dog' && 'cat'     // => 'cat'
null && 'cat'      // => false
'dog' || 'cat'     // => 'dog'
undefined || 'cat' // => 'cat'
!true              // => false
!(7 < 5)           // => true

Comparison Operators

Compare two values and returns a boolean

5 > 3 // => true
5 < 3 // => false
5 >= 3 // => true
5 <= 5 // => true

Equality Operators

5 == '5'        // => true
true == '1'     // => true

5 === '5'       // => false
true === '1'    // => false

'cat' != 'dog'  // => true
'cat' !== 'cat' // => false

Equals vs. Identity

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.


Lab: Compare and Contrast

Open up a node environment in your terminal, and let's play around with some comparison operators.


Assignment Operators

Modify an existing value by some other value.

let x = 7
let y = 3

x += 1 // x => 8
y += 3 // y => 6

x += y // x => 14
y -= x // x => -8

Lab: Assignment

Open a terminal and start node, or create a new JavaScript file.


The Ternary Operator

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'

 Previous Next 

Outline

[menu]

/