DEV Community

Cover image for Navigating JavaScript with Ease: Unveiling `is.null` and `is.not_null` from 'thiis'
Ivan Karbashevskyi
Ivan Karbashevskyi

Posted on

Navigating JavaScript with Ease: Unveiling `is.null` and `is.not_null` from 'thiis'

JavaScript, the realm of endless possibilities, sometimes requires us to decipher the mysteries of null values. Fear not, for the 'thiis' package brings two trusty companions to your aid: is.null and is.not_null. In this delightful journey, we'll explore these tools, uncovering their magic and weaving them into the fabric of everyday JavaScript adventures.

Embracing the Null Quest

Image description

Before we embark on our quest, let's grasp the concept of null in JavaScript. Null is a special value that represents the absence of a value or a variable that has been declared but not assigned any value. Identifying and handling null is crucial for building robust and error-free code.

Meet is.null - Your Null Navigator

Documentation link

Imagine you're on a grand quest to find null values in your code. The is.null method acts as your trustworthy navigator, ensuring that a value is indeed null. Let's witness it in action:

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

const myValue = null;
const result = is.null(myValue);

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.null method to confirm that myValue is indeed null. As expected, it returns true because the value is indeed null.

A Symphony of Examples

Now, let's compose a symphony of examples that showcase the versatility of is.null and its companion, is.not_null. We'll explore six unique scenarios, dancing through various landscapes of JavaScript.

1. Embracing Null Values

The primary role of is.null is to identify null values. Use it to gracefully handle situations where a value might be null:

import { is } from 'thiis';

const potentiallyNullValue = someFunctionThatMayReturnNull();

if (is.null(potentiallyNullValue)) {
  // Handle the null scenario gracefully.
} else {
  // Continue with confidence.
}
Enter fullscreen mode Exit fullscreen mode

2. Guarding Against Null

Conversely, is.not_null is your guardian against null values. Ensure a value isn't null before proceeding:

import { is } from 'thiis';

const importantValue = someFunctionThatShouldNotBeNull();

if (is.not_null(importantValue)) {
  // Your guardian prevents null mishaps!
} else {
  // Time to explore other possibilities.
}
Enter fullscreen mode Exit fullscreen mode

3. Validating Inputs with is.null

When working with user inputs, ensuring they are not null is essential. Use is.null to validate and prompt users to provide valid inputs:

import { is } from 'thiis';

function validateUserInput(input) {
  if (is.null(input)) {
    return 'Please provide a valid input.';
  } else {
    return 'Input validated successfully!';
  }
}
Enter fullscreen mode Exit fullscreen mode

4. Robust Conditionals with is.not_null

Conditionals are at the heart of programming. Ensure a value is not null before executing specific actions with is.not_null:

import { is } from 'thiis';

const userChoice = getUserInput();

if (is.not_null(userChoice)) {
  // Execute actions for valid choices.
} else {
  // Handle other scenarios with grace.
}
Enter fullscreen mode Exit fullscreen mode

5. Stream of Clarity with is.null

Let's embark on a stream adventure with RxJS. Using filter and is.null, ensure that the stream processes only null values:

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

const stream$ = from([null, 'not null', undefined, 'another value', null]);

stream$
  .pipe(
    filter(is.null)
  )
  .subscribe(value => {
    console.log(value); // Only null values will be part of the stream's story.
  });
Enter fullscreen mode Exit fullscreen mode

In this example, the filter(is.null) ensures that only null values get processed by the stream.

6. Array Exploration with is.not_null

Arrays are another playground for is.not_null. Use every() to confirm that all elements are not null and some() to check if at least one isn't:

import { is } from 'thiis';

const notNullArray = [1, 'not null', true];
const mixedArray = [null, 'not null', 0, undefined];

const allElementsNotNull = notNullArray.every(is.not_null); // true
const someElementsNotNull = mixedArray.some(is.not_null); // true

console.log(allElementsNotNull);
console.log(someElementsNotNull);
Enter fullscreen mode Exit fullscreen mode

Here, allElementsNotNull checks if all elements in notNullArray are not null, and someElementsNotNull checks if at least one element in mixedArray is not null.

The Adventure Continues

The is.null and is.not_null methods from the 'thiis' package are your reliable companions on your JavaScript adventure. They make null handling playful and precise, ensuring your code interacts with null values exactly as intended. By adding the 'thiis' package to your JavaScript toolkit and exploring its documentation for more tips and examples, you can navigate the JavaScript landscape with confidence and a touch of fun.

So, keep coding, and remember that JavaScript is a grand adventure waiting to unfold! And, as always, a big thank you to ChatGPT for making this article an enjoyable journey into the world of JavaScript null values.

🎗 ChatGPT & DALL·E 3

Top comments (1)

Collapse
 
karbashevskyi profile image
Ivan Karbashevskyi

Telegram channel:
t.me/thiis_pkg

NPM:
npmjs.com/thiis