> ## Content Index
> Fetch the complete content index at: https://www.oransblog.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# Hidden Javascript features
- URL: https://www.oransblog.com/javascript/
- Published: 2021-12-21T03:57:30.000Z
- Updated: 2026-10-29T05:56:56.000Z
- Description: Here are some uncommon or little known used javascript features const b = template` wait what? `
- Author: Oran C
- Tags: #Import 2026-11-12 01:14

Here are some uncommon or little known used javascript features

updated 20211220  
updated 20211225 svelte addition  
updated 20220125 for spead string tirck

# Tagged Template Literals (es6 only)

from mdn

```bash
Tags allow you to parse template literals with a function. The first argument of a tag function contains an array of string values. The remaining arguments are related to the expressions.

```

source [developer.mozilla.org](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template%5Fliterals?ref=oransblog.com)

If you’ve never seen styled-components before, this weird "button \`\`" syntax looks

```javascript
const Button = styled.button`
  background-color: papayawhip;
  border-radius: 3px;
  color: palevioletred;
`
```

great rightup for how it works

```javascript
const favoriteFood = 'pizza'
const favoriteDrink = 'obi'

logArgs`I like ${favoriteFood} and ${favoriteDrink}.`
// -> ["I like ", " and ", "."] "pizza" "obi"
```

![](https://mxstbr.blog/img/logargs-explanation.png)

from the bellow article

[The magic behind 💅 styled-componentsNever seen that magic backtick styled.div\`\` notation? It’s actually just JavaScript, no fancy transpiler needed! What that is, how it works and what it does? Let’s find out!![](https://mxstbr.blog/favicon.png)<mxstbr/>![](https://mxstbr.blog/img/styled-components-magic.png)](https://mxstbr.blog/2016/11/styled-components-magic-explained/?ref=oransblog.com)

# `?.`Optional chaining 

```javascript
object.with.lots.of.nested.stuff
const adventurer = {
  name: 'Alice',
  cat: {
    name: 'Dinah'
  }
};

const dogName = adventurer.dog?.name;
console.log(dogName);
// expected output: undefined

console.log(adventurer.someNonExistentMethod?.());
// expected output: undefined

```

this will return undefined instead of error out useful when using network javascript objects that may not exist

### [Array item access with optional chaining](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional%5Fchaining?ref=oransblog.com#array%5Fitem%5Faccess%5Fwith%5Foptional%5Fchaining)

```
let arrayItem = arr?.[42];
```

[Optional chaining (?.) - JavaScript | MDNThe optional chaining operator (?.) enables you to read the value of a property located deep within a chain of connected objects without having to check that each reference in the chain is valid.![](https://developer.mozilla.org/favicon-48x48.97046865.png)MDN![](https://developer.mozilla.org/mdn-social-share.0ca9dbda.png)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional%5Fchaining?ref=oransblog.com)

# Nullish coalescing operator (??)

### a safer replacement for `||` when using default values

```javascript
const foo = null ?? 'default string';
console.log(foo);
// expected output: "default string"

const baz = 0 ?? 42;
console.log(baz);
// expected output: 0

```

Mdn saids it best

""""

Earlier, when one wanted to assign a default value to a variable, a common pattern was to use the logical OR operator ([||](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical%5FOR?ref=oransblog.com)):

```
let foo;

//  foo is never assigned any value so it is still undefined
let someDummyText = foo || 'Hello!';

```

However, due to `||` being a boolean logical operator, the left hand-side operand was coerced to a boolean for the evaluation and any *falsy* value (`0`, `''`, `NaN`, `null`, `undefined`) was not returned. This behavior may cause unexpected consequences if you consider `0`, `''`, or `NaN` as valid values.

"""" - [source](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish%5Fcoalescing%5Foperator?ref=oransblog.com#assigning%5Fa%5Fdefault%5Fvalue%5Fto%5Fa%5Fvariable)

# What about \`labels\`?

[label - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/label?ref=oransblog.com)

```javascript
foo: {
  console.log('face');
  break foo;
  console.log('this will not be executed');
}
console.log('swap');

// this will log:

// "face"
// "swap" 

```

# Update! 

### turns out svelte the react replacement UI framework uses templates heavly!

![](https://www.oransblog.com/content/images/2021/12/image-17.png)

Source [Docs • Svelte](https://svelte.dev/docs?ref=oransblog.com)

---

# For of loops with async functions!

[for await...of - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for-await...of?ref=oransblog.com)

```javascript
const asyncIterable = {
  [Symbol.asyncIterator]() {
    return {
      i: 0,
      next() {
        if (this.i < 3) {
          return Promise.resolve({ value: this.i++, done: false });
        }

        return Promise.resolve({ done: true });
      }
    };
  }
};

(async function() {
   for await (let num of asyncIterable) {
     console.log(num);
   }
})();

// 0
// 1
// 2

```

# Raw template Literals aka \`words\`

this allows you to have non escaped control characters! nice!

![](https://www.oransblog.com/content/images/2021/12/image-18.png)

![](https://www.oransblog.com/content/images/2021/12/image-19.png)

(text version of images above)

```javascript
String.raw`works with newlines\n`
'works with newlines\\n'
```

```javascript
sssString.raw`also tabs \t?`
'also tabs \\t?'
```

# Cool trick: String to Character Array

### ever wanted to convert an string into array of characters?   
usefull when you want to do a array operation that is not availbe on strings

## Code

```bash
alphabet = [..."ABCDEFGHIJKLMNOPQRSTUVWXYZ"]
> ["A", "B", "C", "D", "E", "F", "G", "H", "I", …]

```

### Take Note of the `..."abce"` 

![](https://www.oransblog.com/content/images/2022/01/image-123.png)

## Other usefull exampling using this trick

### code bellow gets all the unique characters within a string

```javascript
duplicates = new Set( [..."AAABBBCC"])
> Array(duplicates) //convert back to array of unque values
  ["A", "B", "C"]
or back to string
 Array(duplicates).join("") 
> "ABC"

```

Source: i found this cool hacker while looking at d3.js's tutorial

[Learn D3: Joins / D3 / Observable](https://observablehq.com/@d3/learn-d3-joins?collection=@d3/learn-d3&ref=oransblog.com)

![](https://www.oransblog.com/content/images/2021/12/oran.jpg) 

# Author

```bash
by oran collins
github.com/wisehackermonkey
oranbusiness ╚(•⌂•)╝ gmail (●'◡'●) com
20211220

```