Yearly Membership Special

Get our yearly plan and access to ALL our courses for just $149/y instead of $199/y.

Created by Maximilian Schwarzmüller
April 15, 2020

JavaScript & Type Coercion

# JavaScript & Type Coercion

In JavaScript, like generally in programming, you work with data - with values. And these values have different types. JavaScript requires certain types for certain operations and also freely converts between types. This can lead to unexpected behaviors - so here’s how JavaScript type coercion works (and what it is).

In the above video, we explore why the following code works the way it does work:

console.log([] == 0); // yields true!

if ([]) {
  console.log('This executes!');
}

if (0) {
  console.log('This does NOT execute!');
}

// Why does the first 'if' statement run when the second one doesn't and [] == 0?