DEV Community

biplavmz
biplavmz

Posted on

1

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)

The best way to debug slow web pages cover image

The best way to debug slow web pages

Tools like Page Speed Insights and Google Lighthouse are great for providing advice for front end performance issues. But what these tools can’t do, is evaluate performance across your entire stack of distributed services and applications.

Watch video

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay