DEV Community

Cover image for Navigating the Web with is.browser and is.not_browser in JavaScript: Your Go-To Tools for Browser Checks
Ivan Karbashevskyi
Ivan Karbashevskyi

Posted on • Edited on

Navigating the Web with is.browser and is.not_browser in JavaScript: Your Go-To Tools for Browser Checks

JavaScript, the language of the web, often finds itself in the browser, where websites come to life. But how can you be sure if your code is indeed running in a browser environment? Enter the superheroes of browser detection: is.browser and is.not_browser from the 'thiis' package. In this article, we'll embark on a journey to explore these tools and learn how they can be your guides to ensuring a smooth web experience.

The Web's Playground

Before we dive into the world of browser detection, let's take a moment to appreciate the vast playground that is the web. JavaScript plays a crucial role in adding interactivity and dynamism to websites, making it essential to know when your code is running in a browser environment.

Meet is.browser - Your Browser Detective

Documentation link

Imagine you're crafting code that's destined for the web. The is.browser method acts as your trusty detective, ensuring that your script is indeed running in a browser. Let's see it in action:

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

const isRunningInBrowser = is.browser();

console.log(isRunningInBrowser); // true (if running in a browser)
Enter fullscreen mode Exit fullscreen mode

In this example, we import the "is" object from the "thiis" package and use the is.browser method to confirm whether the code is running in a browser. If it is, the method returns true; otherwise, it returns false.

The Web Journey of Examples

Now, let's embark on a journey through various scenarios where is.browser and is.not_browser can shine. We'll explore six unique examples, including exciting ones.

1. Embracing Browser-Specific Logic with is.browser

When you have logic that's specifically for the browser, you can use is.browser to ensure it runs only in that environment:

import { is } from 'thiis';

if (is.browser()) {
  // Your browser-specific code goes here.
  console.log('Hello, Browser!');
} else {
  console.log('This is not a browser environment.');
}
Enter fullscreen mode Exit fullscreen mode

2. Server-Side Safety with is.not_browser

On the server side, where a browser-specific code might cause issues, you can use is.not_browser for safety:

import { is } from 'thiis';

if (is.not_browser()) {
  // Your server-side code remains safe.
  console.log('Running on the server.');
} else {
  console.log('This is a browser environment.');
}
Enter fullscreen mode Exit fullscreen mode

3. Browser-Only Event Handling with is.browser

When attaching event listeners in the browser, you want to ensure it's happening in a browser environment. Use is.browser for event handling assurance:

import { is } from 'thiis';

if (is.browser()) {
  document.getElementById('myButton').addEventListener('click', () => {
    console.log('Button clicked in the browser!');
  });
}
Enter fullscreen mode Exit fullscreen mode

4. Node.js Server Exclusivity with is.not_browser

In a Node.js environment, where browser-specific code might not make sense, you can use is.not_browser to exclude it:

import { is } from 'thiis';

if (is.not_browser()) {
  // Your Node.js server-side code here.
  console.log('Running in a Node.js environment.');
} else {
  console.log('This is a browser environment.');
}
Enter fullscreen mode Exit fullscreen mode

5. Browser-Safe Stream Handling with is.browser

Let's explore a scenario involving stream$ from RxJS. Using filter and is.browser, we can ensure that the stream processes values only in a browser environment:

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

const clickStream$ = fromEvent(document, 'click');

clickStream$
  .pipe(
    filter(is.browser)
  )
  .subscribe(() => {
    console.log('Click event detected in the browser!');
  });
Enter fullscreen mode Exit fullscreen mode

In this example, the filter(is.browser) ensures that the click event is processed only when the code is running in a browser.

6. Array Navigation with is.not_browser

Arrays can also benefit from browser detection. Use is.not_browser to include code that's relevant outside of the browser:

import { is } from 'thiis';

const serverArray = ['server', 'array', 'elements'];
const browserArray = ['browser', 'array', 'elements'];

const processArray = is.not_browser() ? serverArray : browserArray;

console.log(processArray);
Enter fullscreen mode Exit fullscreen mode

In this case, the is.not_browser() check helps determine which array to process based on the environment.

The Adventure Continues

The is.browser and is.not_browser methods from the 'thiis' package are your trusty companions in the web development journey. They make browser detection playful and precise, ensuring your code interacts seamlessly with the browser environment. By adding the 'thiis' package to your JavaScript toolkit and exploring its documentation for more tips and examples, you can navigate the web landscape with confidence and a sense of exploration.

So, keep coding, and may your web adventures be full of delightful discoveries!

🎗 ChatGPT & DALL·E 3

Top comments (0)