Fundamental JS & Logic

Objectives

By the end of this lesson you should be able to...

Define and describe the purpose of a REPL

Understand the very basics of programming

Evaluate expressions using booleans and arithmetic operators

Agenda

The REPL

Arithmetic Operators

Comparison Operators

Logical Operators

Logic Problems

Agenda

The REPL

Arithmetic Operators

Comparison Operators

Logical Operators

Logic Problems

The REPL

READ

PRINT

EVAL

LOOP

read input from the keyboard

evaluates the code that it reads

run the previous commands until termination 

formats and displays the results

The REPL

wes:~/ $ node
> 10 + 90;
100

Run a REPL w/Node

wes:src/ $ node ./example.js
done.
wes:src/ $

Run a file w/Node

CTRL + D to Quit

or

CTRL + C twice

CTRL + C to cancel

Agenda

The REPL

Arithmetic Operators

Comparison Operators

Logical Operators

Logic Problems

Arithmetic Operators

wes:~/ $ node
> 3 + 3
6
> 6 - 4
2
> 12 / 2
6
> 3 * 11
33
wes:~/ $ node
> 3 + 3 + 3
9
> 3 * 4 + 5
17
> 3 * (4 + 5)
27
> (9 / 3) * (4 + 5)
27

The Basics

Chaining & Precedence

Arithmetic Operators

wes:~/ $ node
> 10 / 2
5
> 10 % 2
0
> 10 / 3
3.3333333333333335
> 10 % 3
1
wes:~/ $ node
> 10 / 3
3.3333333333333335
> Math.ceil(10 / 3)
4
> Math.floor(10 / 3)
3
> Math.round(10 / 3)
3

Modulo (%)

Rounding Up & Down

Agenda

The REPL

Arithmetic Operators

Comparison Operators

Logical Operators

Logic Problems

Comparison Operators

wes:~/ $ node
> 3 > 1
true
> 1 < 10
true
> 12 > 21
false
> 13 < 3
false
wes:~/ $ node
> 3 >= 3
true
> 3 >= 4
false
> 3 <= 3
true
> 3 <= 2
false

The Basics

Compare or Equivalent

Comparison Operators

wes:~/ $ node
> 3 >= ( 4 - 1 )

> 10 <= ( 100 / 20 )

> ( 10 - (15 / 3) + 10 ) > ( 11 * (11 + 2) )

> ( 11 - 1 / 1 ) < ( 100 / ( (25 / 5) + 5 ))

Chaining & Precedence

true
false
false
false

Comparison Operators

wes:~/ $ node
> 10 === 10
true
> 10 === 1
false
> "ABC" === "abc"
false
> "abc" === "abc"
true
wes:~/ $ node
> 3 !== 3
false
> 3 !== 13
true
> "abc" !== "ABC"
true
> "abc" !== "abc"
false

Equivalency

Not Equivalent (Bang!)

Actually a Logical Operator

Comparison Operators

wes:~/ $ node
> 3 == 3

> 3 != 3

> 3 == "3"

> 3 != "3"
wes:~/ $ node
> true === "true"

> true == "true"

> 1 == "true"

> 1 == true

Evil, Evil "=="

Like, seriously?

true
false
true
false
false
false
false
true

"==" attempts to coerce types, sometimes, maybe, if it feels like it

Agenda

The REPL

Arithmetic Operators

Comparison Operators

Logical Operators

Logic Problems

Logical Operators

Logical Operators allow us to check for validity of a statement

 

For example, is the following statement

true or false:

I am both a student in class and not a student in class

Logical Operators

wes:~/ $ node
> true && true
true
> true && false
false
> false && false
false
> false && true
false
wes:~/ $ node
> true || true
true
> true || false
true
> false || true
true
> false || false
false

The Basics of And (&&)

The Basics of Or (||)

The last two evaluates slightly faster because of Short-Circuit Evaluation

The first two evaluate slightly faster because of Short-Circuit Evaluation

Logical Operators

wes:~/ $ node
> ( 3 > 2 ) || false

> ( 3 !== "3" ) && ( 10 < 25 )

> ( 1 !== 0 ) && ( 10 % 3 > 2 )

> true === !( (11 - 1 / 2) > 11 )

Numbers, Chaining, and Bang

true
true
true
false

Logical Operators

wes:~/ $ node
> 1 == true
true
> "" == false
true
> 0 == false
true
> !true === false
true
wes:~/ $ node
> !!0
false
> !!""
false
> !!" "
true
> !!1
true

...?

Truthiness & Falseness

All types have inherent truthiness or falseness, which you can check with "!!"

Agenda

The REPL

Arithmetic Operators

Comparison Operators

Logical Operators

Logic Problems

Logic Problems

wes:~/ $ node
> false || !!false || !false

> true && !( 10 > 10 ) && !( 10 % 2 )

> ( 11 - 9 ) && !( "3" !== "3" ) && !!1024

> (!!0 || !!"" || !!false) === !!0

> !( 100 * (10 % 5) ) || !( (3 !== "3") && !(false) )

> !"user is not signed up" &&
... !!"wants to purchase" ||
... !!"can receive a free trial"
true
true
true
true
true
true

Logic Problems

Create 5 logic puzzles on your own and then trade them with a neighbor

 

Try your best to stump them and don't evaluate them with the console!

Fundamental JavaScript & Logic

By Wes Reid

Fundamental JavaScript & Logic

  • 719