DEV Community

Paul
Paul

Posted on

7 JavaScript One-Liners That Will Make You Look Like a Pro

Today we will be talking about some JavaScript cool tricks. These aren't just random code hacks – they're real solutions that can seriously clean up your programming.

1. The Object Property Shorthand

Remember those old-school JavaScript days when we'd write out every single property?

const name = 'Sarah';
const age = 28;
const user = { name: name, age: age };
Enter fullscreen mode Exit fullscreen mode

Here's a better way to do it:

const user = { name, age };
Enter fullscreen mode Exit fullscreen mode

Gone are those clunky lines where you'd repeat yourself.

2. Swapping Variables Without a Temporary One

Need to swap two variables? Use this:

[a, b] = [b, a];
Enter fullscreen mode Exit fullscreen mode

The first time you see this, you'll do a double-take. Array destructuring turns variable swapping into something that looks almost too good to be true. No temp variables, no complex logic – just pure, elegant code.

3. The Nullish Coalescing Operator: Your New Best Friend

Remember writing those lengthy default value checks?

const userPreference = input !== null && input !== undefined ? input : 'default';
Enter fullscreen mode Exit fullscreen mode

Here's how the cool kids do it now:

const userPreference = input ?? 'default';
Enter fullscreen mode Exit fullscreen mode

This little ?? operator has saved me from so many headaches. It only falls back to the default when the value is null or undefined – not when it's 0 or an empty string.

4. The Ultimate Array Deduplication

If you want to make your array unique, do this:

const unique = [...new Set(array)];
Enter fullscreen mode Exit fullscreen mode

If it was before we'd use loop but now I just smile and spread that Set anytime I need it.

5. Converting to Boolean Like a Boss

Instead of writing value === true or Boolean(value).

Here's a sure better way:

const isTrue = !!value;
Enter fullscreen mode Exit fullscreen mode

Those double exclamation marks might look like you're really excited about something, but they're actually doing some clever type coercion.

6. Quick Number Conversion That Actually Makes Sense

If you want to convert a string to a number? Forget parseInt() for a second, use this instead:

const number = +'42';
Enter fullscreen mode Exit fullscreen mode

That lonely plus sign is doing all the heavy lifting here and it accurately converts a string number to a number.

7. The Smooth Operator (Optional Chaining)

Don't do this again:

const userCity = user && user.address && user.address.city;
Enter fullscreen mode Exit fullscreen mode

A much more better way:

const userCity = user?.address?.city;
Enter fullscreen mode Exit fullscreen mode

This one feels like JavaScript finally understood our pain and decided to do something about it.

The Bottom Line

Coding shortcuts represent more than just fewer keystrokes.

They're about writing smarter, more intentional software. These techniques separate average developers from great coders. Use them wisely, sparingly, and with purpose.

Great code tells a story. It should be clear, powerful, and elegant. Don't just chase brevity – chase understanding.

That's all, now tell me what's your favorite JavaScript trick?

Share it with me even though you knew it before you read this article.

Top comments (0)