DEV Community

biplavmz
biplavmz

Posted on

How to Avoid JavaScript Type Conversions (Type Coercion)

JavaScript performs implicit conversions between data types. Let’s find out how to avoid it.

Have you ever experienced a scenario where certain value comparisons in JavaScript do not result in what you expected?

`if([]){
console.log("working");
}

if(0){
console.log("working for 0");
}

if([]==0){
console.log(true);
}
`

Even though []==0 results in true , the if condition has not executed according to that result. Ever wondered why that is?

*What are JavaScript Type Conversions?
*

it is simply the automatic conversion of values from one data type to another.
This occurs because JavaScript is a weakly typed language.

Use strict comparison (===) when comparing values we can Avoid JavaScript Type Conversions

Top comments (0)