Navigating the Symbolic World with is.symbol and is.not_symbol from 'thiis': Unraveling JavaScript Magic
Tags: beginners, programming, codenewbie, frontend
Published: 2023-11-23 11:07:33

JavaScript, like magic, is full of enchanting elements, and symbols are one such mystical feature. Symbols are unique and can be both fascinating and tricky to work with. Fear not! With the magical tools `is.symbol` and `is.not_symbol` from the 'thiis' package, you can confidently navigate the symbolic world of JavaScript. In this article, we'll explore these tools through friendly examples, revealing the magic behind symbols.

## The Magic of Symbols in JavaScript


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oru3fj89pfwishd6kws4.jpeg)

Before we embark on our magical journey, let's understand what symbols are. In JavaScript, symbols are a primitive data type, and each symbol created is unique. They are often used as keys for object properties, adding a touch of magic to your code.

## Meet `is.symbol` - Your Symbolic Guide

[Documentation link](https://karbashevskyi.github.io/thiis/docs/API/Common/is.symbol)

Picture yourself in a realm where symbols abound, and you want to identify if a value is indeed a symbol. The `is.symbol` method acts as your magical guide, confirming whether a value is a symbol or not. Let the magic unfold:

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

const magicalSymbol = Symbol('magic'); // Creating a symbolic value
const result = is.symbol(magicalSymbol);

console.log(result); // true
```

In this example, we import the "is" object from the "thiis" package and use the `is.symbol` method to confirm that `magicalSymbol` is indeed a symbol. As expected, it returns `true`, unveiling the magic of symbols.

## The Journey of Examples

Now, let's embark on a magical journey with six enchanting examples, including one with `stream$` and another involving an array.

### 1. **Discovering the Symbolic Secrets**

The primary role of `is.symbol` is to unveil the symbolic nature of a value. It helps you uncover the magic when working with symbols:

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

const mysteriousSymbol = someFunctionThatMayReturnSymbol();

if (is.symbol(mysteriousSymbol)) {
  // You've discovered the symbolic secret!
} else {
  // Continue your journey with anticipation.
}
```

### 2. **Guarding Against Non-Symbols**

Conversely, `is.not_symbol` serves as a guardian against non-symbolic values. It ensures that a value isn't a symbol before proceeding, protecting your magical realm:

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

const importantValue = someFunctionThatShouldNotBeSymbol();

if (is.not_symbol(importantValue)) {
  // Your guardian prevents non-symbolic mishaps!
} else {
  // Time to explore other magical possibilities.
}
```

### 3. **Creating Magical Constants**

Symbols are often used to create unique constants. Use `is.symbol` to validate that a constant is indeed a magical symbol:

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

const MAGIC_CONSTANT = Symbol('magic_constant');

if (is.symbol(MAGIC_CONSTANT)) {
  // Embrace the magic of your constant!
} else {
  // Handle the non-symbolic scenario gracefully.
}
```

### 4. **Magical Stream Filtering**

Let's dive into a magical stream scenario with RxJS. Using `filter` and `is.symbol`, we can ensure that the stream processes only values that are symbols:

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

const magicalStream$ = from([42, 'not a symbol', Symbol('magic'), { obj: 'not a symbol' }]);

magicalStream$
  .pipe(
    filter(is.symbol)
  )
  .subscribe(value => {
    console.log(value); // Only magical symbols will be part of the stream's tale.
  });
```

In this example, the `filter(is.symbol)` ensures that only symbolic values get processed by the magical stream.

### 5. **Enchanting Array Exploration**

Arrays are another dimension for `is.not_symbol`. You can use `every()` to confirm that all elements are not symbols and `some()` to check if at least one isn't:

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

const notSymbolArray = [1, 'not a symbol', true];
const magicalArray = [Symbol('magic'), 'not a symbol', 42];

const allElementsNotSymbol = notSymbolArray.every(is.not_symbol); // true
const someElementsNotSymbol = magicalArray.some(is.not_symbol); // true

console.log(allElementsNotSymbol);
console.log(someElementsNotSymbol);
```

Here, `allElementsNotSymbol` checks if all elements in `notSymbolArray` are not symbols, and `someElementsNotSymbol` checks if at least one element in `magicalArray` is not a symbol.

### 6. **Magical Object Keys**

Symbols are often used as unique keys for object properties. Use `is.symbol` to ensure that a property key is truly magical:

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

const magicalKey = Symbol('magic_key');
const obj = {
  [magicalKey]: 'Abracadabra!',
  ordinaryKey: 'Just a key',
};

if (is.symbol(Object.getOwnPropertySymbols(obj)[0])) {
  console.log(obj[magicalKey]); // Unleash the magic of the object property!
} else {
  // Handle the non-symbolic property with grace.
}
```

## The Magical Adventure Continues

The `is.symbol` and `is.not_symbol` methods from the 'thiis' package are your magical companions on your JavaScript journey. They make working with symbols a delightful experience, ensuring your code interacts with these enchanting values precisely as intended. By adding the 'thiis' package to your JavaScript toolkit and exploring its documentation for more tips and examples, you can navigate the symbolic landscape with confidence and a dash of magic.

So, keep coding, and may your JavaScript be filled with enchantment! A special thanks to ChatGPT for making this article a magical experience.

> 🎗 ChatGPT & DALL·E 3