1 of 53

Explore the JavaScript Universe

Rebuild your mental model from the inside out.

2 of 53

Mental Models

01

3 of 53

let a = 10;

let b = a;

a = 0;

Run this in your brain 🧠

What are the values of a and b?

4 of 53

Coding, Fast and Slow

Responsible for complex step-by-step reasoning

Good at pattern matching and “gut reactions”

Slow Thinking System

Fast Thinking System

5 of 53

function duplicateSpreadsheet(original) {

if (original.hasPendingChanges) {

throw new Error('Save the file before duplicate it!');

}

let copy = {

author: original.author,

cells: original.cells,

metadata: original.metadata,

};

copy.metadata.title = 'Copy of ' + original.metadata.title;

return copy;

}

Run this in your brain 🧠

6 of 53

The JavaScript

Universe

02

7 of 53

What is a “value” ?

8 of 53

9 of 53

10 of 53

Values

Objects and functions are also values but, unlike primitive values, I can manipulate them from my code

They are a permanent part of our JavaScript universe. I can point to them, but I can’t create, destroy, or change them.

Object & Functions

Primitive Values

11 of 53

Types of Values

Primitive Values

Objects and Functions

  • undefined, used for unintentionally missing values.
  • null, used for intentionally missing values.
  • Booleans ( true & false), used for logical operations.
  • Numbers (-100, 3.14, and others), used for math calculations.
  • Strings ("hello", "abracadabra"), used for text.
  • Objects ({} and others), used to group related data and code.
  • Functions (x => x * 2 and others), used to refer to code.

12 of 53

Checking a Type

console.log(typeof(2)); // "number"

console.log(typeof("hello")); // "string"

console.log(typeof(undefined)); // "undefined"

console.log(typeof({})); // "object"

console.log(typeof([])); // "object"

console.log(typeof(x => x * 2)); // "function"

13 of 53

Expressions

What is an “Expressions” ?

14 of 53

console.log(2 + 2);

Run this in your brain 🧠

console.log(typeof(2));

15 of 53

Expressions

Expressions are questions that JavaScript can answer with values.

Expressions always result in a single value.

16 of 53

Values and Variables

03

17 of 53

let reaction = 'yikes';

reaction[0] = 'l';

console.log(reaction);

Run this in your brain 🧠

It will prints "yikes" instead of “likes

18 of 53

Primitive Values Are Immutable

“Immutable” is a fancy Latin way to say “unchangeable.”

19 of 53

let arr = [212, 8, 506];

console.log(arr[0]); // 212

arr[0] = 420;

console.log(arr); // [420, 8, 506]

Run this in your brain 🧠

let str = 'hello';

console.log(str[0]); // "h"

str[0] = 'j'; // ???

20 of 53

let fifty = 50;

fifty.shades = 'gray';

console.log(fifty);

Run this in your brain 🧠

21 of 53

In our JavaScript universe, all primitive values are distant stars, floating farthest from our code. We can point to them, but they will always stay where they are, unchanged.

22 of 53

let pet = 'Narwhal';

pet = 'The Kraken';

console.log(pet);

Run this in your brain 🧠

Variables are not values.

Variables point to values.

23 of 53

Variables Are Wires

let pet = 'Narwhal';

pet = 'The Kraken';

console.log(pet);

24 of 53

let pet = 'Narwhal';

pet = 'The Kraken';

console.log(pet);

25 of 53

20000 = 'under the sea';

'war' = 'peace';.

Rule of the assignment

pet = count + ' Dalmatians';

The left side of an assignment must be a “wire”

The right side of an assignment must be an expression, so it always results in a value

26 of 53

Reading a Value of a Variable

console.log(pet);

When we write pet, we’re asking JavaScript a question: “What is the current value of pet?”

27 of 53

Who cares if you say “pass a variable” or “pass a value”?

28 of 53

function double(x) {

x = x * 2;

}

let money = 10;

double(money);

console.log(money); // ?

Run this in your brain 🧠

29 of 53

Let’s Revisit

30 of 53

let x = 10;

let y = x;

x = 0;

Quiz#0 excalidraw.com 🧠

Draw what happens to the “wires” of the x and y variables step by step

31 of 53

let x = 10;

let y = x;

x = 0;

32 of 53

let x = 10;

let y = x;

x = 0;

Variables always point to values

33 of 53

let x = 10;

let y = x;

x = 0;

34 of 53

Studying from the Inside

04

35 of 53

Is our JavaScript universe really real?

36 of 53

Studying from the outside

37 of 53

Studying from the inside

38 of 53

Meeting the Primitive Values

05

39 of 53

40 of 53

Undefined

// "undefined"

console.log(typeof(undefined));

41 of 53

Undefined

let bandersnatch;

console.log(bandersnatch); // undefined

42 of 53

null

43 of 53

null

let mimsy = null;

console.log(mimsy.mood); // TypeError!

44 of 53

Boolean

45 of 53

Boolean

console.log(typeof(true)); // "boolean"

console.log(typeof(false)); // "boolean"

46 of 53

Quiz#1

let isSad = true;

let isHappy = !isSad;

let isFeeling = isSad || isHappy;

let isConfusing = isSad && isHappy;

Draw what happens to the “wires” variables step by step on Excalidraw.com

47 of 53

Strings

48 of 53

In our universe, there is a distinct value for every conceivable string.

49 of 53

To be continued …

50 of 53

Quiz #2

import feed from './feed.js';

let pets = 'Tom and Jerry';

feed(pets);

console.log(pets[0]);

Your colleague bets they can change your console.log output by only editing their feed.js file. Do you believe them, or would you bet they can’t?

51 of 53

Quiz #3

let meals = 4;

let wheels = meals;

let eels = 2 + 2;

Draw what happens to the “wires” variables step by step on Excalidraw.com

52 of 53

Reference

JustJavaScript.com

53 of 53

Thanks!

Do you have any questions?