DEV Community

Cover image for Mastering JavaScript One-Liners: Part 2
Raju ghorai
Raju ghorai

Posted on

Mastering JavaScript One-Liners: Part 2

In the first part of our journey towards mastering JavaScript one-liners, we explored some nifty tricks to simplify common coding tasks. Now, we're back with even more one-liners that will make your code shorter, sweeter, and more efficient. So, buckle up, JavaScript enthusiast, and let's dive right into the world of concise coding!

8) Check if an Array Contains a Specific Element

Traditional approach:

let numbers = [1, 2, 3, 4, 5];
let target = 3;
let containsElement = false;
for (let i = 0; i < numbers.length; i++) {
   if (numbers[i] === target) {
     containsElement = true;
     break;
   }
}

Enter fullscreen mode Exit fullscreen mode

One-liner efficiency:

let numbers = [1, 2, 3, 4, 5];
let target = 3;
let containsElement = numbers.includes(target);

Enter fullscreen mode Exit fullscreen mode

The one-liner employs the includes method to quickly check if an array contains a specific element. This approach not only reduces code length but also enhances readability.

9) Find the Maximum Value in an Array

Traditional approach:

let numbers = [1, 7, 3, 9, 5];
let max = numbers[0];
for (let i = 1; i < numbers.length; i++) {
   if (numbers[i] > max) {
     max = numbers[i];
   }
}
Enter fullscreen mode Exit fullscreen mode

One-liner elegance:

let numbers = [1, 7, 3, 9, 5];
let max = Math.max(...numbers);
Enter fullscreen mode Exit fullscreen mode

By using the spread operator and Math.max, the one-liner succinctly finds the maximum value in an array. It's a prime example of how one-liners can simplify complex operations.

10) Generate an Array of Sequential Numbers

Traditional approach:

let start = 1;
let end = 5;
let sequentialArray = [];
for (let i = start; i <= end; i++) {
   sequentialArray.push(i);
}
Enter fullscreen mode Exit fullscreen mode

One-liner finesse:

let start = 1;
let end = 5;
let sequentialArray = Array.from({ length: end - start + 1 }, (_, i) => start + i);
Enter fullscreen mode Exit fullscreen mode

The one-liner utilizes the Array.from method to generate an array of sequential numbers efficiently. It showcases how one-liners can simplify tasks that involve array creation.

11) Check if a String Contains a Substring

Traditional approach:

let text = "Hello, world!";
let substring = "world";
let containsSubstring = text.indexOf(substring) !== -1;
Enter fullscreen mode Exit fullscreen mode

One-liner simplicity:

let text = "Hello, world!";
let substring = "world";
let containsSubstring = text.includes(substring);
Enter fullscreen mode Exit fullscreen mode

The one-liner uses the includes a method to check if a string contains a specific substring. It provides a more intuitive and concise way to perform this common task.

12) Find the Average of Array Elements

Traditional approach:

let numbers = [1, 2, 3, 4, 5];
let sum = 0;
for (let i = 0; i < numbers.length; i++) {
   sum += numbers[i];
}
let average = sum / numbers.length;
Enter fullscreen mode Exit fullscreen mode

One-liner efficiency:

let numbers = [1, 2, 3, 4, 5];
let average = numbers.reduce((acc, num) => acc + num, 0) / numbers.length;
Enter fullscreen mode Exit fullscreen mode

The one-liner combines the reduce method and division to find the average of array elements succinctly. It demonstrates how one-liners can optimize your code structure.

These additional JavaScript one-liners showcase the power of concise coding techniques, enhancing code clarity and efficiency across various common tasks. By incorporating these into your coding practices, you'll not only improve your productivity but also create more maintainable code.

Embrace the elegance of one-liners and continue your journey to becoming a JavaScript coding maestro. Remember, the key to mastering JavaScript one-liners is practice. As you encounter different coding challenges, look for opportunities to apply these techniques and watch your coding skills reach new heights. If like this content you can find more series of artiles here.
Happy coding!

Top comments (0)