You are currently viewing All Possible False Values In JavaScript?

All Possible False Values In JavaScript?

Hello Spectaculars,

Welcome to this article of ServiceNow Spectaculars.

In this article, we dive deep into the world of JavaScript, and discuss all possible false values received during coding.

What is the meaning of False value in JavaScript?

In JavaScript, a falsy value is a value that is considered false when evaluated in a Boolean context. When any non-Boolean value is used in a Boolean context, such as in an if statement or as the condition for a loop, it is automatically coerced into a Boolean value. A falsy value is one that is coerced to false.

Till today JavaScript has six falsy values.

Knowing which values are falsy in JavaScript can be useful for writing conditional statements that correctly handle different types of input, especially when working with user input or external data sources where the data may not always be in the expected format or type.

List Of all possible false values?

In JavaScript, the following values are considered falsy, meaning they are treated as false in a Boolean context:-

  1. false
  2. 0 (zero)
  3. '' or "" (empty string)
  4. null
  5. undefined
  6. NaN (Not a Number)

Any other value that is not on this list is considered truthy, meaning it is treated as true in a Boolean context.

Meaning & code example of all these values?

  • false: This is a Boolean value that represents false.
Sample Code:

var isFalse = false;
if (!isFalse) {
  gs.info("This code will execute because isFalse is false.");
}
Output:

*** Script: This code will execute because isFalse is false.

  • 0: This is a numeric value that represents zero.
Sample Code:

var num = 0;
if (!num) {
  gs.info("This code will execute because num is zero.");
}
Output:

*** Script: This code will execute because num is zero.

  • '' or "": These are empty strings.
Sample Code:

var str = '';
if (!str) {
  gs.info("This code will execute because str is an empty string.");
}
Output:

*** Script: This code will execute because str is an empty string.
  • null: This is a special value that represents the absence of any object value.
Sample Code:

var obj = null;
if (!obj) {
  gs.info("This code will execute because obj is null.");
}
Output:

*** Script: This code will execute because obj is null.

  • undefined: This is a special value that represents the absence of any value.
Sample Code:

var varUndefined;
if (!varUndefined) {
  gs.info("This code will execute because varUndefined is undefined.");
}
Output:

*** Script: This code will execute because varUndefined is undefined.

  • NaN (Not a Number): This is a special value that represents the result of an operation that cannot produce a normal numeric value.
Sample Code:

var result = parseInt("hello");
if (isNaN(result)) {
  gs.info("This code will execute because result is not a number.");
}
Output:

*** Script: This code will execute because result is not a number.

Leave a Reply