Truthy Falsy in JavaScript

Truthy Falsy in JavaScript | The JavaScript language has a few peculiarities we need to be aware of, and comparing two items to see if they are equal will frequently catch the unwary developer off guard.

This tutorial will examine why that is while exploring the double and triple equals operators and the idea of truthy and falsy values in JavaScript. When you’re done reading, you’ll know how JavaScript compares objects and how truthy and falsy values can make your code more readable. Let us see what are truthy and falsy values in JavaScript.

Falsy in JavaScript

A value regarded as false when it appears in a boolean environment is known as a falsy value (sometimes spelled falsy).

Any value can be converted to a Boolean via type conversion in JavaScript, such as in conditionals and loops. The complete list of JavaScript false values is shown below:-

  • false:- false is the keyword.
  • 0:- The digit zero (so, also 0.0, etc., and 0x0).
  • -0:- The negative number is zero (so, also -0.0, etc., and -0x0).
  • 0n:- Large integer zero (so, also 0x0n). The negation of 0n equals 0n; therefore, note that no BigInt negative zero exists.
  • "":- The empty string value.
  • null:- The lack of any values is null.
  • undefined:- The fundamental value is undefined.
  • NaN:- Not a number
  • document.all:- If and only if an object has the internal [[IsHTMLDDA]] slot, it is considered false. That slot is exclusive to the document.all, JavaScript cannot be used to set anything.
if ("") {
  console.log("The input is truthy");
} else {
  console.log("The input is falsy");
}

Output:-

The input is falsy

Truthy in JavaScript

A truthy value in JavaScript is a value that is treated as true when it appears in a Boolean context. Unless they are declared false, all values are true. All values are truthy, except for false, 0, -0, 0n, "", null, undefined, and NaN.

In Boolean cases, type coercion is used in JavaScript. JavaScript truthy values (which will be forced to be true in boolean contexts and thus trigger the if block) includes the following values:-

  • ‘0’ (a string containing a single zero)
  • ‘false’ (a string containing the text “false”)
  • [ ] (an empty array)
  • { } (an empty object)
  • function(){} (an “empty” function)
if (1) {
  console.log("The input is truthy");
} else {
  console.log("The input is falsy");
}

Output:-

The input is truthy

Difference Between Truthy Falsy in JavaScript

Each value has a type and an underlying Boolean value, often either truthy or falsy. Some rules governing the conversion of non-Boolean values into true or false values are somewhat strange. Troubleshooting JavaScript apps helps one understand the principles and how they affect the comparison.

You can create concise JavaScript conditions and ternary operators using truthy and falsy values. However, always take edge cases into account. An errant empty array or NaN variable could cause numerous hours of troubleshooting!

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *