How to Test If a Value is a Real Value in JavaScript

Whenever you want to see if a value is actually real instead of just undefined or null, you can use undefined or null to compare it.

You can use either the equal to operator (==) and the not equal to operator (!=) for the test.

So thanks to JavaScript’s automatic conversions work like this:

console.log(0 == false);
//true

or like this

console.log("" == false);
//true

You see that 0 converts into false and “” converts into false thanks to automatic conversion.

How to stop JavaScript from converting types

If you don’t want JavaScript to automatically convert types while using comparison operators, you can use the three-character comparisons: === or !==.

These operators no longer allow JavaScript to make any conversions.

console.log(0 === false);
//false

=== tests whether the two are exactly alike.

console.log('' !== false);
//false

!== tests whether the two are not exactly alike.

These operators can help prevent unexpected conversions. Use it when you are sure when the types of both values are exactly the same.

That’s unless you just want to test whether they are the same or not.

Leave a Comment