Skip to content

Latest commit

 

History

History
100 lines (75 loc) · 3.02 KB

Truthy-and-Falsy.md

File metadata and controls

100 lines (75 loc) · 3.02 KB

What are Truthy and Falsy values in JavaScript?

In JavaScript, every value has an inherent Boolean value, referred to as truthy or falsy. This determines whether the value is treated as true or false when evaluated in a Boolean context, such as an if statement, loops, or ternary operators.


Falsy Values

Falsy values are treated as false when encountered in a Boolean context. There are exactly six falsy values in JavaScript:

  1. false – The Boolean false itself.
  2. 0 – The number zero.
  3. "" or '' – An empty string (both single and double quotes).
  4. null – Represents the absence of any value.
  5. undefined – A variable that has been declared but not assigned a value.
  6. NaN – "Not-a-Number," typically the result of an invalid mathematical operation.

Examples of Falsy Values in Action:

if (!false) console.log("Falsy: false");
if (!0) console.log("Falsy: 0");
if (!"") console.log("Falsy: Empty String");
if (!null) console.log("Falsy: null");
if (!undefined) console.log("Falsy: undefined");
if (!NaN) console.log("Falsy: NaN");

Output:

Falsy: false
Falsy: 0
Falsy: Empty String
Falsy: null
Falsy: undefined
Falsy: NaN

Truthy Values

All other values apart from the six falsy values are considered truthy. These are values that are treated as true in a Boolean context. Some common truthy values include:

  1. Non-zero numbers (e.g., 42, -1, 3.14).
  2. Non-empty strings (e.g., "hello", 'false', " " – a string with just a space).
  3. Arrays (e.g., [] – even an empty array is truthy).
  4. Objects (e.g., {} – even an empty object is truthy).
  5. true – The Boolean true itself.
  6. Special values like Infinity and -Infinity.

Examples of Truthy Values in Action:

if (42) console.log("Truthy: Non-zero number");
if ("hello") console.log("Truthy: Non-empty string");
if ([]) console.log("Truthy: Array");
if ({}) console.log("Truthy: Object");
if (true) console.log("Truthy: true");
if (Infinity) console.log("Truthy: Infinity");

Output:

Truthy: Non-zero number
Truthy: Non-empty string
Truthy: Array
Truthy: Object
Truthy: true
Truthy: Infinity

Why Are Truthy and Falsy Values Important?

Truthy and falsy values are integral to JavaScript's dynamic nature. They are used in conditions and logic checks to simplify code and handle edge cases. For example:

Using Falsy Values for Default Assignment:

let name = ""; // Empty string (Falsy)
let defaultName = name || "Anonymous"; // Default to "Anonymous" if name is falsy
console.log(defaultName); // Output: Anonymous

Using Truthy Values for Conditions:

let isLoggedIn = true; // Truthy value
if (isLoggedIn) {
    console.log("Welcome back!");
}

Conclusion

Understanding truthy and falsy values in JavaScript helps in writing concise and efficient code. Knowing how JavaScript evaluates these values ensures fewer bugs and cleaner logic in your programs.