The && and || operator

The && and || operator in Javascript has some quirks. It's best we clarify what it actually does since it's not easy to intuit for newcomers to the language despite being idiomatic.

The && and || evaluate to the value that is either to the left or right of the operator. (Not necessarily a true or false.)

What && does:

const truthy = 1;
const falsey = null;

console.log(truthy && falsey); // null
console.log(falsey && truthy); // null

If the left expression is falsey (false when coerced to a boolean), then return the left expression as the statement return value.

  1. Otherwise (if the left expression of && is truthy,) return the right side expression as the statement return value.

And for ||:

const truthy = 1;
const falsey = null;

console.log(truthy || falsey); // 1
console.log(falsey || truthy); // 1
  1. If the left expression is truthy, then return the left expression as the statement return value.
  2. If the left expression is falsey, then return the right expression as the statement return value.

Avoid this variable initialization pitfall

There's a few different ways we can write functions in Javascript.

First, there is the function declaration:

function declaredFunction() {
  // ...

Then there is a function expression that is referenced by a variable:

var functionExpression = function() {
  // ...

This largely seems like the same thing, but what's happening under the hood is a bit more nuanced:

In the first example, the function declaration is hoisted to the top of the executing function scope; This means both the variable declaredFunction and the function body is defined before the containing function executes. (Note: in this example, we don't have a containing function so the hoisting would happen whenever the program executes.)

In the second example, only the variable declaration functionExpression is hoisted to the top of the executing function scope. This means functionExpression has a value of undefined when the program begins execution, and it is only when the runtime gets to the line with the = where functionExpression is assigned a value that the variable functionExpression actually points to a value.

The second is how, I think, most people understand these 2 seemingly identical programs to execute. Here's an exercise to help further clarify the difference:

What are the values logged by the two examples below?


function declaration() {
  // ...

var expression = function() {
  // ...

In the first example, it's the entire function function declaration() {...} but in the second example, it's undefined.

Again, the function declaration is hoisted to the top of the executing function scope before the runtime begins execution. This means the variable declaration is defined and assigned a value before program execution.

The variable expression in the second example is hoisted to the top, but its assignment happens when the runtime gets to the assignment operator ( =)  during execution. Hence, the console.log(expression) is undefined.

Alternatively, if you use const instead of var, the runtime will give you an Uncaught ReferenceError: Cannot access 'expression' before initialization.

I guess it's just another case of why we should use const and let instead of var; It has better, more predictable runtime behavior.

Javascript Variables: var, let, and const

What is a variable?

Imagine an address for a physical home. An address references a physical location.

But that address – the letters and numbers that make up an address' representation – is not necessarily tied to a physical location. If the streets and buildings were completely rebuilt with a new layout, the old addresses wouldn't mean much as the streets would likely be laid out differently. There would have to be new addresses made to point to the new physical locations of points of interest. This is similar to how variables work in a computer program.

In Javascript, we can assign a variable to a value like so:

var entree = 'pasta';

This specifies that we have an address entree that is assigned a value of pasta. But it doesn't necessarily mean that the entree can't be reassigned to reference some other value. All I would need to do is:

entree = 'pizza';

If we don't want to allow a variable to change the value that it references, we can use the const keyword instead:

const entree = 'pasta';
entree = 'pizza'; // This statement is illegal now.

The let behaves similarly to how var would behave before the ES6 standard in that it allows for reassignment of the variable to different values.

The nuanced difference is that a let variable is block scoped. The simplest way to explain this is with an example:

if (true) {
  var entree = 'pasta';  
console.log(entree); // pasta

if (true) {
  let dessert = 'tiramisu';
console.log(dessert); // Error: dessert is not defined.

In practice, I always use const and let. I have not found any practical reason for using the less restrictive var.

The Ordinary Moments Matter

I just watched this Tim Ferris interview with Brené Brown and I was compelled to transcribe her words down in a blog post because it was remarkable.

Yes... I get to do extraordinary things... but we are all ordinary people. But sometimes, this world is tough because we shame and diminish ordinary: Ordinary lives or small lives. We chase extraordinary moments instead of being grateful.
Until... until... Hard-shit happens. In the face of really hard stuff – illness, death, loss — the only thing we're begging for is a normal moment. Can I please have that normal moment back? Can I please hear him come through the screen porch door? Can I please get a call from my mom or a crazy text?
Then we want those ordinary moments. But in them, with all the noise, it's about the extraordinary right now.

It's so easy to let this moment slip by unnoticed.

It's so easy to think that none of it really matters.

But it's good to keep in mind that our time is limited, and their time might be too. Every interaction we have with the friend in front of us, in that very moment, is a beautiful moment that is to be appreciated. It's all in the now. It's all about the ordinary moments right now.

Take a moment to be appreciative. Thanks to Tim and Brené for sharing this conversation.

Here's the video for reference:

Why We Should All Be More Appreciative

Everyone wants to be happy.

Well, not necessarily, but I've never met anyone who wants to be unhappy so I'll assume that everyone does want to be happy. (I think there's a logical fallacy in there somewhere.)

Appreciation is the feeling of happiness. I think happiness and appreciation is the same thing, but described in a way that makes their similarity difficult to spot.

When we feel content, it's because we are appreciative of what we have. We feel happy because we understand that we have everything we need in life.

Appreciation is described as an act. Therefore it is understood as something that can be induced by choice. e.g. "Be Appreciative."

Happiness, on the other hand, is described as something we feel and an effect of our outside circumstances. It's not that this framing around happiness is exactly incorrect, but it is a little misleading, and this framing makes it so elusive to achieve.

"What makes you happy?" What a difficult question to answer. It is so difficult to answer because happiness is a result of what we do, not a result of what we receive.

To put it more precisely, happiness is a result of our feelings towards our outside circumstances. But what happens to us (something we receive) is not the same thing as how we interpret what happens to us (something we do).

How we feel about something is largely controlled by the narrative we decide to use to describe the events that occur to us.

"I have to take out the trash. It's so much extra work to tie up the heavy trash bag and haul it across the yard."

"I get to take out the trash and feel the sense of accomplishment of tidying up my house."

"I have to walk the dog. There goes another 15 minutes of my time."

"I get to walk the dog. Not everyone has this privilege of being able to walk, or own a dog."

"I have to go home and give my baby a bath. I'm tired from work and all I really want to do is relax."

"I get to go home and give my baby a bath. I am fortunate to have a job that allows me to go home early enough to spend quality time with family and give my baby a bath. I have the time and space to spend time with people I love."

Both interpretations are accurate but one will make you happier than the other. Why pick the negative one? We have a choice.

The reality is that being appreciative is a practice we can always employ, and happiness is the feeling that results from the practice.

The alternative would be to practice ignoring all the things we could be appreciative for, focus only on things that make us unhappy, and get stuck in a loop wondering why nothing good comes our way.

Of course nothing good comes our way if we don't practice appreciation. We'll miss out on everything good that happens to us.

Everything good is already around us if we just practice appreciation. We don't even have to wait for good things to happen. We can begin experiencing happiness right now.