Skip to content

TIL - Eloquent JavaScript Chapter 2 - 3

Posted on:February 11, 2024 at 12:25 PM

Note: Eloquent JavaScript was recommended to me by Leon from #100Devs. While I’ve been participating in a JavaScript study using The Modern JavaScript Tutorial, I had an itch to also read Eloquent JavaScript as a refresher.

Eloquent JavaScript TIL - Chapter 2 to 3.

Chapter 2 - Program Structure

Expression vs. statement always confuses me so I took it upon myself to refer to Josh Comeau’s post on the difference between statements and expressions.

// Examples of expressions:

1
true
"hello"
10 > 20 // produces false
name ? 'anonymous' : 'boyeon'
// examples of statements 
const name = 'boyeon' // this is instructing the program to create a variable and assign it a string value

if (name) { // this is telling the program to do something if the name value exists 
//...
}

Control flow

Chapter 3 - Functions

Closure

// Code from MDN's Closure

function init() {
  var name = "Mozilla"; // name is a local variable created by init
  function displayName() {
    // displayName() is the inner function, that forms the closure
    console.log(name); // use variable declared in the parent function
  }
  displayName();
}
init();