False Values in JavaScript

Grasping these fundamentals is essential for anyone seeking to understand the underlying reasons behind JavaScript's behavior. With this knowledge, you will enhance your ability to diagnose problems more efficiently, as you will have insight into the reasons behind their malfunctioning. This understanding will also facilitate your progression from a junior developer to an intermediate one.

In JavaScript, every value possesses an intrinsic Boolean value. As a result, it is essential to comprehend whether these values will evaluate to true or false, particularly when engaging in comparisons and utilizing conditional statements.

Conditional Statements with False Value

Every value in JavaScript is capable of evaluating to a boolean, specifically either true or false. Consequently, the condition used in the if statement can be simplified.

if(false === null)

if(undefined === null)

if(null === null)

if(NaN === null)

if(0 === null)

if("" === null)

if(value === null)

Making use of False Values in Logical Operators

Here’s an intriguing point. To yield a value, utilize the operators || and &&. It is essential to grasp which values are considered false and true.

|| Once an honest statement is evaluated, the process of comparison will cease.

Example

const or = '' || 'hello';

const or = [] || 'hello';

&& The evaluation of a false expression will immediately terminate the comparison.

Example

const and = '' && 'hello';

const and = [] && 'hello'

Community Case Studies

Utilizing erroneous values in unit testing

Example

[false, undefined, NaN].forEach(el =>

expect(funcImTesting(el).to.be('hello world')),

);

Next, we will examine each of them individually by selecting a specific example for each case;

1. False:

False represents a boolean value indicating the lack of truth. A simple comparison utilizing the === operator can be employed to check for false:

Code:

Example

if (false === false) {

console.log("This statement will be executed.");

}

Output:

Output

This statement will be executed.

2. undefined:

An uninitialized variable or property is denoted by the value undefined. To check for undefined, you can utilize strict equality:

Code:

Example

let variable;

if (variable === undefined) {

console.log("This statement will be executed.");

}

Output:

Output

This statement will be executed.

3. null:

The object value null represents the intentional lack of any object values. The equality operator serves as a means to verify whether a value is null:

Code:

Example

let value = null;

if (value === null) {

console.log("This statement will be executed.");

}

Output:

Output

This statement will be executed.

4. NaN:

"Not a Number" (NaN) is a unique representation utilized to signify an irrational or undefined value within mathematical computations. The term itself stands for "Not a Number." To determine if a value is NaN, the isNaN function can be employed:

Code:

Example

let result = Math.sqrt(-1);

if (isNaN(result)) {

console.log("This statement will be executed.");

}

Output:

Output

This statement will be executed.

5. 0 (zero):

The value 0 is commonly interpreted as false. You can utilize a conditional statement to verify whether a value is 0:

Code:

Example

let number = 0;

if (number === 0) {

console.log("This statement will be executed.");

}

Output:

Output

This statement will be executed.

6. "" or the empty string ""

The value False corresponds to an empty string, which can be represented as '' or ". You can utilize the length property to check if a string has no content:

Code:

Example

let string = '';

if (string.length === 0) {

console.log("This statement will be executed.");

}

Output:

Output

This statement will be executed.

7. expression value === null

When a variable holds a value that is precisely equal to null, the expression value === null confirms this condition. In such a case, the expression returns true, indicating that the value is indeed null. Conversely, if the variable contains any other value or is undefined, the expression will return false.

To illustrate the concept of the null value in JavaScript, take a look at the example below:

Code:

Example

let variable1 = null;

let variable2 = "Hello";



if (variable1 === null) {

console.log("This statement will be executed.");

}

if (variable2 === null) {

console.log("This statement will not be executed.");

}

Output:

Output

This statement will be executed.

In the earlier example, the initial if statement will execute because the value of variable1, which is null, meets the condition of variable1 === null. Conversely, the second if statement will not execute since variable2 contains the string "Hello", which does not equate to null.

Input Required

This code uses input(). Please provide values below: