🤓7 Useful JavaScript One-Liner Code You Must Know 💎
Tags: webdev, javascript, programming, beginners
Published: 2023-12-06 13:54:34

**Hey Coders**, it's me [Md Taqui Imam](mdtaquiimam.vercel.app). **In today's blog**, I want to share some 7 useful JavaScript one-liner code that can make your code look more professional and reduce code base size.

---

**And Don't forget to checkout 👇 once**
{% embed https://htmlrev.com/ %}

---

So Let's begin and don't forget to **drop a (🔥💖🦄🚀🤯)** and **save it** for later 📌.

_If you have any doubt then comment below 💌._

{% cta https://github.com/taqui-786 %}Follow me on Github✅{% endcta %}

---


## 1. Find Value in Array 🔍
Let's start with Basic One-Liner of how to Check if an Array contain a value or not.

```js
// Array
const MyArray = [12,55,40,78,5];
// One-liner 
const haveValue = arr => arr.includes(5);
// Test
console.log("Answer ", haveValue(MyArray)) // Answer true

```
This is the one-liner to see Array contain a Particular value or not. You can modify accourding to your needs.

## 2. Object Deep Clone 💀
This one-Liner will make a Deep clone of your object:

```js
const myObj = {
  "name": "Md Taqui Imam",
  "age": 22,
  "country": "india"
};

const deepClone = obj => JSON.parse(JSON.stringify(obj));

console.log("Answer ", deepClone(myObj));

// Answer Object {"name": "Md Taqui Imam","age": 22,"country": "india"}
```
And **thanks to @jonrandy** contribuiting about **one more shortest and easier** way to clone objects in javascript **with** [structuredClone( )](https://developer.mozilla.org/en-US/docs/Web/API/structuredClone)

```js
const myObj = {
  "name": "Md Taqui Imam",
  "age": 22,
  "country": "india"
};

const deepClone = structuredClone(myObj) // with  structuredClone

console.log("Answer ", deepClone);

```



## 3. Scroll To Top 📃
This is the One-Liner that instantly navigates you to the Top of the Page :

```js
window.scrollTo(0, 0) // navigate to Top
```

### 4. Truncate a String To a Specific Length "..."⭐
This One-Liner is to Truncate a String and add "..." to an End on a specific given Length :

```js
const myString = "Lorem ipsum is simply dummy text of the printing and typesetting industry.";

const truncate = (str, len) => str?.length > len ? str.slice(0, len) + "..." : str;

console.log("Answer", truncate(myString, 52)); 
// Answer Lorem ipsum is simply dummy text of the printing and...
```

## 5. Remove Duplicated item in 👉 [ ]
This one-Liner will return a new Array without repeating duplicated item from an Array :

```js
const myArr = [4,8,3,9,4];

const duplicate = arr => [...new Set(arr)]

console.log("Answer ", duplicate(myArr));

 // Answer (4) [4, 8, 3, 9]
```

## 6. Check all are same 🤔 or not
This one-liner will help you to check that all item of an Array are same/identical or Not :

```js
const myArr = [4,4,4,4,4];

const allSame = arr => arr.every(val => val === arr[0]);

console.log("Answer ", allSame(myArr));

 // Answer true
```

## 7. Get last item from 👉 [ ]
Now, Last one-liner will help you to get the last item of an array :

```js
const myArr = [4,8,2,9,1,6];

const lastItm = arr => arr[arr.length - 1];

console.log("Answer", lastItm(myArr));

 // Answer 6
```

---

## See you in next week 😅
I hope you find this Blog post helpful and these 7 one-liners will help you to make your code base size smaller, and boost you productivity.

**Have a nice day👋**


{% embed https://dev.to/random_ti %}

[![github](https://img.shields.io/badge/GitHub-100000?style=for-the-badge&logo=github&logoColor=white)](https://github.com/taqui-786) 

[![twitter](https://img.shields.io/badge/Twitter-1DA1F2?style=for-the-badge&logo=twitter&logoColor=white)](https://twitter.com/Taquiimam14)

[![portfolio](https://img.shields.io/badge/Portfolio-255E63?style=for-the-badge&logo=About.me&logoColor=white)](https://mdtaquiimam.vercel.app)

[![buymeacoffee](https://img.shields.io/badge/Buy_Me_A_Coffee-FFDD00?style=for-the-badge&logo=buy-me-a-coffee&logoColor=black)](https://www.buymeacoffee.com/taquiDevloper)