Academind Logo

JavaScript & Type Coercion

JavaScript uses values of certain types and it freely converts between those types. That's called 'Type Coercion' and it can be tricky + lead to bugs

Created by Maximilian Schwarzmüller
#

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?

Recommended Courses