Truthy vs Falsy values in the JavaScript
Do you know that Javascript is a scripting programming language that conforms to the ECMAScript ? Did you know JavaScript is both an object-oriented as well as a functional programming language? If you didn’t then you must know by searching on Google. In this article, I will move over some interesting yet important JS programming concepts.
- Truthy & Falsy ?
In JavaScript, truthy are expressions which evaluates to boolean true
value and falsy evaluates to boolean false
value. Unlike other languages, true
and false
values are not limited to boolean
data types and comparisons. It can have many other forms.
Every number in JavaScript is truthy except “0”. If a variable contains “0”, then it’s a falsy value.
Lets learn what makes truthy and falsy expressions in JavaScript.
let x = 1;
if(x){
console.log("Condition is true");
}
else{
console.log("Condition is false");
}// Output: Condition is true
if you see this result on the console, that will be You are truthy
let x = 0;
if(x){
console.log("Condition is true");
}
else{
console.log("Condition is false");
}// Output: Condition is false
in that case, the result will be You are Falsy. Ok That was simple. Everyone knows that 0 is boolean false and 1 is true. So what’s a great deal about it ??
Let’s see another example:
let x;
if(x){
console.log("Condition is true");
}else{
console.log("Condition is false");
}// Output: Condition is false
Guess the result now. If you see this on the console, you will find the result is You are Falsy. Hmm…That’s interesting. So why does that happen? In javascript, any variable whose value is not defined is known as an undefined value, and an undefined value has a negative like impression in JS. that is why it’s caught as Falsy value. There are a few more Falsy expression in javascript. They are:
The following values are always falsy:
false
0
(zero)''
or""
(empty string)null
undefined
NaN
Everything else is truthy. That includes:
'0'
(a string containing a single zero)'false'
(a string containing the text “false”)[]
(an empty array){}
(an empty object)function(){}
(an “empty” function)
Anything other than those is truthy value in Javascript. Now, let’s talk about some of the Falsy keywords
- Null & undefined value?
As we have seen in the variable section that we can assign any primitive or non-primitive type of value to a variable. JavaScript includes two additional primitive type values — null and undefined, that can be assigned to a variable that has special meaning
const a = 50;
if (a === null) {
return (“I am empty”);
}else{ return a;}
// Output: Condition is false
const test = (p) => {
if (p === undefined) {
return 'Undefined value!';
}
return p;
}let t;console.log(test(t));// output: "Undefined value!"
- The “NaN”?
The NaN is a property of the global object. In other words, it is a variable in global scope. Get’s complicated ?? Let’s make it simpler.
The NaN (Not a NUMBER) is a special type of keyword that use to check if a value is a number or not. It not only checks whether the value is empty “null” or “undefined”, but also checks if the value is a NUMBER or not. In javascript, The isNaN() function often used to check whether a value is “NaN” or not.
Interestingly is, since NaN is the only JavaScript value that is treated as unequal to itself, you can always test if a value is NaN by checking it for equality to itself. An Example:
var a = NaN;a !== a;// Output: true
That’s it for this article. Hope you like those simple information about Javascript.
- The ( ==) & ( === ) operator?
JavaScript it’s important to know which operator is used for which operation. As I said that, === takes type of variable in consideration, while == make type correction based upon values of variables, following are couple of more differences between “==” and “===” operator in JavaScript programming language :. For example:
Let number = “1000”;
if (number == 1000) }
console.log(“Both are equal”);
}
else {
console.log(“Both are not equal”);
}
// Output: Both are not equal
Though the 100 is a string, the result will be “Both are equal”. Because the == doesn’t Compare the type of values, only checks the value. But when,
Let number = “1000”;
if (number === 1000) }
alert(“Both are equal”);
}
else {
alert(“Both are not equal”);
}
// Output: Both are equal
The results will be “Both are not equal”. Because the === does Compare the type of values, and when the type and value both match, it gives the compression result is TRUE. Hope that clears the confusion about the two operators.