DEV Community

Cover image for Mastering `is.boolean` for Type Checking in JavaScript with 'thiis': Fun Examples and Useful Tips
Ivan Karbashevskyi
Ivan Karbashevskyi

Posted on • Edited on

Mastering `is.boolean` for Type Checking in JavaScript with 'thiis': Fun Examples and Useful Tips

JavaScript is a dynamic and versatile programming language that's known for its flexibility. As a developer, it's essential to make sure your code can handle different data types accurately. In this article, we'll explore the is.boolean method provided by the "thiis" package, a fantastic tool designed to help you check if a value is a boolean with ease.

Understanding Booleans in JavaScript

Before we dive into the exciting world of the is.boolean method, let's quickly brush up on what booleans are in JavaScript. Booleans are a special type of data with just two possible values: true and false. They're like the "on/off" switches of your code and play a crucial role in decision-making.

Meet the is.boolean Method

Documentation link

The is.boolean method is not a native JavaScript feature, but it's a handy tool provided by the "thiis" package. It simplifies the process of checking if a given value is a boolean. This comes in super handy when you need to make sure a variable or input meets the criteria of being a boolean value.

https://karbashevskyi.github.io/thiis/docs/API/Boolean/is.boolean

Let's see how the is.boolean method works in action:

import { is } from 'thiis'; // Import the "is" object from the "thiis" package

// Using the `is.boolean` method
const myBoolean = true;
const result = is.boolean(myBoolean);

console.log(result); // true
Enter fullscreen mode Exit fullscreen mode

In this example, we import the "is" object from the "thiis" package and use the is.boolean method to check if myBoolean is a boolean. As you might expect, it returns true.

Real-World Scenarios with Fresh Examples

Now, let's dive into some practical examples to see how the is.boolean method can make your coding life easier and more fun.

1. Form Validation

Imagine you're working on a form, and you want to check if a checkbox or radio button is checked. You can use the is.boolean method to handle this:

import { is } from 'thiis';

const formInput = document.querySelector('#checkboxInput');
const isChecked = is.boolean(formInput.checked);

if (isChecked) {
  // Celebrate the checked state!
} else {
  // Handle the unchecked state with grace.
}
Enter fullscreen mode Exit fullscreen mode

2. Dealing with API Responses

When you're working with APIs, data can come in all shapes and sizes. With the is.boolean method, you can ensure that a specific property in the API response is a valid boolean:

import { is } from 'thiis';

fetch('https://api.example.com/data')
  .then(response => response.json())
  .then(data => {
    const isValid = is.boolean(data.isActive);

    if (isValid) {
      // Party time for the isActive property!
    } else {
      // Deal with the unexpected like a champ.
    }
  });
Enter fullscreen mode Exit fullscreen mode

3. Rocking Conditional Logic

Before you unleash your conditional statements, make sure the condition you're evaluating is a bona fide boolean:

import { is } from 'thiis';

const isUserLoggedIn = true;

if (is.boolean(isUserLoggedIn)) {
  // Your code for handling a boolean condition can shine.
} else {
  // Handle other cases gracefully.
}
Enter fullscreen mode Exit fullscreen mode

4. Data Transformations

When you're juggling data, use the is.boolean method to ensure that specific data points meet your criteria, especially when you're doing data gymnastics:

import { is } from 'thiis';

function transformData(data) {
  if (is.boolean(data.isFeatured)) {
    // Shine the spotlight on data.isFeatured.
  } else {
    // Handle the rest of the data gracefully.
  }
}
Enter fullscreen mode Exit fullscreen mode

5. Array Adventures

The is.boolean method can also accompany you on your array-checking adventures. Use every() to make sure all elements are booleans and some() to check if at least one isn't:

import { is } from 'thiis';

const booleanArray = [true, false];
const mixedArray = [true, false, 0];

const areAllBooleans = booleanArray.every(is.boolean); // true
const hasNonBoolean = mixedArray.some(is.not_boolean); // true

console.log(areAllBooleans);
console.log(hasNonBoolean);
Enter fullscreen mode Exit fullscreen mode

6. Stream Stories

If you're into streams and libraries like RxJS, the is.boolean method is your trusted companion. Use the filter() operator to ensure that your stream only emits glorious boolean values:

import { is } from 'thiis';
import { from } from '

rxjs';
import { filter } from 'rxjs/operators';

const stream$ = from([true, false, 'not a boolean', true, 123, false]);

stream$
  .pipe(
    filter(is.boolean)
  )
  .subscribe(value => {
    console.log(value); // Only the true and false values will join the party!
  });
Enter fullscreen mode Exit fullscreen mode

In this scenario, we create an observable stream (stream$) that emits various values. The filter(is.boolean) operator ensures that only boolean values are invited to the party, and the subscribe function logs their adventures.

Wrapping It Up

The is.boolean method from the "thiis" package is your trusty sidekick in the world of JavaScript development. It streamlines type checking and ensures your code plays nice with the data types you expect. By adding the "thiis" package to your JavaScript projects and exploring its documentation for more tips and examples, you can become a master of precise type checking in JavaScript, all while having fun. The new examples provided in this article showcase the versatility and power of this method in real-world scenarios. Enjoy using it on your coding adventures!

🎗 ChatGPT & DALL·E 3

Top comments (1)

Collapse
 
karbashevskyi profile image
Ivan Karbashevskyi

Telegram channel:
t.me/thiis_pkg

NPM:
npmjs.com/thiis