Javatpoint Logo
Javatpoint Logo

False Values in JavaScript

Understanding these foundations is crucial if you want to comprehend why JS behaves the way it does. You will be able to troubleshoot issues more effectively since you will understand why they aren't working, and it will help you advance from being a junior to an intermediate developer.

In JS, every value has an underlying Boolean value. Therefore, it's crucial to understand whether those numbers would be true or false, especially when using comparisons and conditionals.

Conditional Statements with False Value

All values in JS evaluate to booleans, therefore (true or false). Our conditional in the if statement can be condensed.

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 one that's fascinating. To return a value, use the operators || and &&. It is crucial to understand what values are false and true.

|| As soon as a truthful expression is assessed, comparisons will come to an end.

&& As soon as a false expression is evaluated, the comparison will end.

Community Case Studies

Utilizing erroneous values in unit testing

Now we will check one by one by taking one example for each of them;

1. False:

False is a boolean value that denotes the absence of truth. You may use a straightforward comparison with the === operator to verify for false:

Code:

Output:

This statement will be executed.

2. undefined:

A variable or property that hasn't been initialized is represented by the value undefined. You can use strict equality to look for undefined:

Code:

Output:

This statement will be executed.

3. null:

The object value null stands for the purposeful absence of all object values. The equality operator can be used to check for null:

Code:

Output:

This statement will be executed.

4. NaN:

"Not a Number" (NaN), a special number used to denote an absurd or undefinable value in mathematical operations, is an abbreviation for "Not a Number". The isNaN() function can be used to check for NaN:

Code:

Output:

This statement will be executed.

5. 0 (zero):

The number 0 is regarded as being false. You can use a conditional statement to check for 0:

Code:

Output:

This statement will be executed.

6. "" or the empty string ""

False is an empty string, denoted by '' or ". The length property can be used to determine whether a string is empty:

Code:

Output:

This statement will be executed.

7. expression value === null

If a variable's value is exactly equivalent to null, the expression value === null verifies this. If so, the expression evaluates to true, proving the value is null. Otherwise, the expression evaluates to false if the value has any other value or is undefined.

To demonstrate the false value null in JavaScript, consider the following example:

Code:

Output:

This statement will be executed.

The first if statement in the preceding example will be carried out since variable1's value of null satisfies the test that variable1 === null. Due to the presence of the string value "Hello" in variable2, which is not equal to null, the second if statement will not be carried out.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA