# 8 Extremely Powerful JavaScript Hacks

Eight surprisingly useful JavaScript patterns, from destructuring tricks to short-circuit logic, that make day-to-day frontend code faster and tighter.

Published April 1, 2024 by Liviu Lupei on the Endtest blog. Canonical URL: https://endtest.io/blog/8-extremely-powerful-javascript-hacks

---
I love to be efficient.

But users don't care about my optimized code if the site isn't working in their Internet Explorer 11 browser.

You can use [Endtest](https://endtest.io) to build automated tests and execute them on the cross-browser cloud.

Below are 8 extremely powerful JavaScript hacks.

## 1. Replace all

We know that the `string.replace()` function replaces only the first occurrence.

You can replace all the occurrences by adding `/g` at the end of the regex.

```js
var example = "potato potato";
console.log(example.replace(/pot/, "tom"));
// "tomato potato"

console.log(example.replace(/pot/g, "tom"));
// "tomato tomato"
```

## 2. Extract unique values

We can create a new array only with the unique values by using the `Set` object and the `Spread` operator.

```js
var entries = [1, 2, 2, 3, 4, 5, 6, 6, 7, 7, 8, 4, 2, 1];
var unique_entries = [...new Set(entries)];
console.log(unique_entries);
// [1, 2, 3, 4, 5, 6, 7, 8]
```
## 3. Convert number to string
We just have to use the concatenation operator with an empty set of quotation marks.

```js
var converted_number = 5 + "";
console.log(converted_number);
// 5
console.log(typeof converted_number);
// "string"
```

## 4. Convert string to number
All we need is the + operator.

Be careful with this one since it only works with "string numbers".

```js
the_string = "123";
console.log(+the_string);
// 123

the_string = "hello";
console.log(+the_string);
// NaN
```

## 5. Shuffle elements from array
**Every day I'm shufflin'**

```js
var my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9];
console.log(my_list.sort(function() {
  return Math.random() - 0.5
}));
// Example output: [4, 8, 2, 9, 1, 3, 6, 5, 7]
```

## 6. Flatten multidimensional array
Simply by using the Spread operator.

```js
var entries = [1, [2, 5], [6, 7], 9];
var flat_entries = [].concat(...entries);
console.log(flat_entries);
// [1, 2, 5, 6, 7, 9]
```

## 7. Short circuit conditionals
Let's take this example:

```js
if (available) {
  addToCart();
}
```

And shorten it by using the variable together with the function:

```js
available && addToCart()
```

## 8. Dynamic property names
I always thought that I first had to declare an object before being able to assign a dynamic property.

```js
const dynamic = 'flavour';
var item = {
  name: 'Coke',
  [dynamic]: 'Cherry'
}
console.log(item);
// { name: "Coke", flavour: "Cherry" }
```

## Conclusion
I think it's really cool that you're looking for JavaScript hacks, but are you sure your Web Application is working correctly on all browsers and devices?

You can use [Endtest](https://endtest.io) to quickly create Automated Tests and execute them on the cross-browser cloud.

You don't even have to code to use it.

Check out the [docs](https://endtest.io/docs/web-tests/how-to-create-web-tests).
