Skip to content
Shameem's Note
Shameem Reza JavaScript, WordPress & SwiftUI
Shameem's Note

JavaScript, WordPress & SwiftUI

BUY ME A COFFEE
  • Home
  • About
  • Now
Shameem's Note

JavaScript, WordPress & SwiftUI

January 23, 2023January 26, 2023

Mastering the Concept of Truthy and Falsy Values in JavaScript

JavaScript is a widely used programming language for creating interactive websites and web applications. One of the key concepts in JavaScript is understanding the difference between truthy and falsy values. In this post, we’ll explore what truthy and falsy values are and how to use them effectively in your code.

What are Truthy and Falsy Values in JavaScript?

In JavaScript, a truthy value is any value that evaluates to true when used in a boolean context. For example, the number 1, the string “hello”, and the array [1, 2, 3] are all truthy values.

On the other hand, a falsy value is any value that evaluates to false when used in a boolean context. Some common falsy values include the number 0, the empty string “”, and the special keyword null.

It’s important to note that not all truthy and falsy values are as straightforward as the examples above. For example, the number -1 is a truthy value, even though it’s not a positive number. Similarly, the keyword undefined is a falsy value, even though it’s not a specific “falsy” value like 0 or an empty string.

How to Use Truthy and Falsy Values in JavaScript?

One of the most common ways to use truthy and falsy values in JavaScript is in conditional statements. For example, consider the following if-else statement:

let x = 0;
if (x) {
  console.log("x is truthy");
} else {
  console.log("x is falsy");
}

In this example, the variable x is assigned the value 0, which is a falsy value. Therefore, the code inside the else block will be executed, and the string “x is falsy” will be logged to the console.

Another way to use truthy and falsy values is with the logical operators && and ||. For example, the following code will return “hello” if the variable name is truthy, and “world” if it’s falsy:

let name = "John";
console.log(name && "hello" || "world");

In this example, the expression name “hello” will evaluate to “hello” because the variable name is truthy. Therefore, the entire expression will be evaluated to “hello” and returned.

How to check if a value is truthy or falsy JavaScript?

In JavaScript, you can check if a value is truthy or falsy by using the if statement or the Boolean() function.

Using the if statement:

let x = 0;
if (x) {
  console.log("x is truthy");
} else {
  console.log("x is falsy");
}

In this example, the variable x is assigned the value 0, which is a falsy value. Therefore, the code inside the else block will be executed, and the string “x is falsy” will be logged to the console.

Using the Boolean() function:

let x = 0;
console.log(Boolean(x)); //outputs false

The Boolean() function is a built-in JavaScript function that returns the boolean value of the given expression. If the value passed to it is a truthy value, it will return true; if the value passed is falsy, it will return false.

What are the falsy values in JavaScript?

In JavaScript, several values are considered “falsy” which evaluates to false when used in a boolean context. These are:

  • false: the keyword false is a boolean value that is considered falsy.
  • 0: the number 0 is falsy in JavaScript.
  • “” (empty string): an empty string is considered falsy.
  • null: the keyword null is considered falsy.
  • undefined: the keyword undefined is considered falsy.
  • NaN (Not a Number): the special value NaN is considered falsy.

It’s important to note that any value that is not included in this list is considered “truthy” and will evaluate to true when used in a boolean context.

It’s worth noting that any object that is truthy in JavaScript, for example, an empty object {} or an empty array [] are considered a truthy value, even though they are empty.

Is {} truthy or Falsy JS?

In JavaScript, an empty object {} is considered a truthy value. This is because, in JavaScript, all objects are considered truthy values and will evaluate to true when used in a boolean context.

It means that if you use an empty object in a conditional statement or a logical operator, it will be treated as a true value.

if({}){
 console.log("This is truthy value")
}

In this example, the if statement will execute and log “This is truthy value” to the console.

It’s worth noting that JavaScript has several falsy values such as null, undefined, NaN, 0, ”, false as well as several truthy values like -1, ‘false’, [], {}, function(){}, etc. So, it’s a good practice to use these methods when you need to check the truthy or falsy value of a variable.

Conclusion

Truthy and falsy values are fundamental concepts in JavaScript and are used in many different ways in the language. By understanding what truthy and falsy values are and how to use them effectively, you’ll be well on your way to becoming a proficient JavaScript developer.

For more information on truthy and falsy values in JavaScript, you can check out the following resources:

  • MDN Web Docs – Truthy and Falsy
  • W3Schools – JavaScript Booleans
  • Script Fundamentals – Truthy and Falsy

These resources provide in-depth explanations, examples, and interactive tutorials to help you understand and master truthy and falsy values in JavaScript. Remember, practice makes perfect, so try experimenting with truthy and falsy values in your own code to gain a deeper understanding of how they work.

Happy Scripting!

JavaScript

Post navigation

Previous post
Next post

Woth to read

JavaScript Mastering the Art of Server-Side Rendering with Next.js

Mastering the Art of Server-Side Rendering with Next.js

January 27, 2023January 27, 2023

Server-side rendering (SSR) is a technique for rendering a client-side JavaScript application on the server…

Read More
JavaScript Strict vs Loose Equality in JavaScript

Strict vs Loose Equality in JavaScript: Understanding the Differences and When to Use

January 20, 2023January 26, 2023

JavaScript has two ways to check for equality between values: strict equality (===) and loose…

Read More
JavaScript jquery plugin shameem

Top List of Useful and Handy jQuery Plugin

June 12, 2018October 15, 2019

jQuery Plugin is lightweight and very easy to use. Using just a few Kb file,…

Read More
Shameem Reza

I am the Father of a son, blessed with a pious wife. I am a self-taught iOS developer, SwiftUI Addicted and WordPress Ninja.

Over 80,000+ Readers

Get fresh content from Shameem.

  • Twitter
  • GitHub
  • LinkedIn
SwiftUI
JavaScript
Wordpress
Tips & Tricks

Recommended Resources

WordHero: 50+ AI writing tool to write sales and marketing emails that sell.

Rytr: AI Writing Tool to generate high-quality content.

Wave.video: Live Streaming Studio, Video Editor, Thumbnail Maker, Video Hosting, Video Recording, and Stock Library combined in one platform.

Ocoya: An all-in-one marketing solution that helps you effortlessly create, automate, and manage high-converting campaigns.

Join 3500+ Developers Getting Early Access To My Posts!

I learned these in the past 10 years by building a digital product development agency, and dozens of different web & mobile applications for clients and myself. Sign up to get updates when I write something new. No spam ever.

© 2023 Shameem's Note | Contact - Privacy - Disclosure