Top 7 JavaScript One-Liners: Your Path to Cleaner, Readable Code 📜
Tags: webdev, javascript, react, beginners
Published: 2023-09-16 11:03:49

Hi, in this article, we are going to discuss how a one-liner in JavaScript can improve our code quality and readability.

First, let's discuss what one-liners in JavaScript are.

This is not a JavaScript keyword or anything specific in JS; it's just a term that refers to accomplishing coding tasks in a single line, instead of using multiple lines or blocks.

For a better explanation, I am going to first write code in the traditional, or less efficient, way and then demonstrate how one-liners come to the rescue.

## 1) Sum of an array of elements 

```js
let numbers = [1, 2, 3, 4, 5];
let sum = 0;
for (let i = 0; i < numbers.length; i++) {
  sum += numbers[i];
}

```

one-liner 

```js
let numbers = [1, 2, 3, 4, 5];
let sum = numbers.reduce((acc, num) => acc + num, 0);
```

## 2) Removing Duplicates from an Array

```js
let numbers = [1, 2, 2, 3, 4, 4, 5];
let uniqueNumbers = [];
for (let i = 0; i < numbers.length; i++) {
  if (!uniqueNumbers.includes(numbers[i])) {
    uniqueNumbers.push(numbers[i]);
  }
}

```

one-liner 

```js
let numbers = [1, 2, 2, 3, 4, 4, 5];
let uniqueNumbers = [...new Set(numbers)];

```

## 3) Swap Two Varaibles 

```js
 let a = 10;
 let b = 20;
 let temp = a;
  a =b;
  b = temp;
 
```
one-liner 
``` js
[b, a] = [a, b]
```


## 4) Reverse a string 

```js
let str = 'hello';
let reversedStr = '';
for (let i = str.length - 1; i >= 0; i--) {
  reversedStr += str[i];
}

```

one-liner 

```js
let str = 'hello';
let reversedStr = str.split('').reverse().join('');

```

## 5) Count Occureance of elements in array

```js
let numbers = [1, 2, 2, 3, 4, 4, 5];
let count = {};
for (let i = 0; i < numbers.length; i++) {
  if (count[numbers[i]]) {
    count[numbers[i]]++;
  } else {
    count[numbers[i]] = 1;
  }
}

```

One-liner:

```js
let numbers = [1, 2, 2, 3, 4, 4, 5];
let count = numbers.reduce((acc, num) => {
  acc[num] = (acc[num] || 0) + 1;
  return acc;
}, {});

```

## 6) Checking if an Object Contains a Specific Key

```js
let myObject = { name: 'Alice', age: 30 };
let hasKey = false;

for (let key in myObject) {
  if (key === 'age') {
    hasKey = true;
    break;
  }
}

```

One liner 

```js
let myObject = { name: 'Alice', age: 30 };
let hasKey = 'age' in myObject;


```

## 7)  Find Intersection of Two Array

```js
let array1 = [1, 2, 3, 4];
let array2 = [3, 4, 5, 6];
let intersection = [];

for (let i = 0; i < array1.length; i++) {
  for (let j = 0; j < array2.length; j++) {
    if (array1[i] === array2[j]) {
      intersection.push(array1[i]);
      break;
    }
  }
}

```

One liner

```js
let array1 = [1, 2, 3, 4];
let array2 = [3, 4, 5, 6];
let intersection = array1.filter(value => array2.includes(value));

```
Hope you liked this article, The above examples come into our day-to-day life. Hope when it comes next you will use a one-liner for that ;)

If you are on Twitter, can you follow me there as well? I post these kinds of stuff there as well. => <a href="https://twitter.com/kushal_js?ref_src=twsrc%5Etfw" class="twitter-follow-button" data-size="large" data-show-count="false">Follow @kushal_js</a><script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>

{% twitter 1703431207676227934 %}