JavaScript & Type Coercion

External video

Load YouTube video?

This video is provided by YouTube. Loading it will connect your browser to YouTube and Google servers, where their own terms and privacy policies apply.

Open on YouTube

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?