Photo by RetroSupply on Unsplash
JavaScript - IF Condition - Part 4
Miscellaneous Conditional Expressions
Video Tutorial
Miscellaneous Truthy and Falsey expressions
In this blog, I will discuss various conditional expressions that I have not discussed in the first three parts.
Case 1: The function returning an undefined value is evaluated as Falsey.
function Fx() {
console.log("This is Fx");
}
if (Fx())
console.log("Call is - Truthy");
else
console.log("Call is - Falsey");
Case 2: The function returns an integer. The conditional expression is evaluated as Truthy.
function Fx() {
return 100; // return - fractional number - result : Truthy
}
if (Fx())
console.log("Fx() - Truthy");
Case 3: The function returns a null value. The conditional expression is evaluated as Falsey.
function Fx() {
return null
}
if (Fx())
console.log("Fx() - Truthy");
else
console.log("Fx() - Falsey");
Case 4: When a conditional expression is an array object. It is evaluated as Truthy.
let arr = [12,2,34]
if(arr)
console.log("array - Truthy");
Case 5: When a conditional expression is an object. It is evaluated as Truthy.
let myobj = {
a:10,
b:"Date"
};
if(myobj)
console.log("object - Truthy");
Case 6: When a conditional expression is a function object. It is evaluated as Truthy.
function Fx() {
console.log("This is a Fx function");
}
if (Fx) // Not calling the function
console.log("Function object - Truthy")
Complete Code Listing
/*
Author: Mahavir DS Rathore
Objective: Miscellaneous Conditional Expressions
*/
function Fx() {
return 10;
}
function Fx2() {
return 20.1;
}
function Fx3() {
return null;
}
function Fx4() {
console.log("This is Fx");
}
if (Fx())
console.log("Fx() - true");
if(Fx2())
console.log("Fx2() - true");
if(Fx3())
console.log("Fx3() - true");
else
console.log("Fx3() - false"); // false
if (Fx4())
console.log("Call is - Truthy");
else
console.log("Call is - Falsey"); // false
if (Fx)
console.log("Fx - true");
var arr = [1,2,3];
if (arr)
console.log("arr - true");
var obj = {
a : 12,
b : "Ram"
};
if (obj)
console.log("obj - true");