DEV Community

Cover image for Top 50 One-Liners JavaScript

Top 50 One-Liners JavaScript

Sh Raj on February 13, 2024

Mastering JavaScript: Top 50 One-Liners Every Developer Should Know Know More :- https://codexdindia.blogspot.com/2024/02/top-50-one-liners-javas...
Collapse
 
manchicken profile image
Info Comment hidden by post author - thread only accessible via permalink
Mike Stemle

So many of these are wrong. #5 will return true for Date and #13 actually takes more work to use as a one liner than it would just use String.includes().

Collapse
 
jonrandy profile image
Info Comment hidden by post author - thread only accessible via permalink
Jon Randy 🎖️ • Edited

They're wrong because they're most likely straight out of ChatGPT (or similar), and the author has not checked them before posting. Try entering the following into ChatGPT:

Write an article titled "Mastering JavaScript: Top 50 One-Liners Every Developer Should Know"

You'll get many of the same examples, with the same mistakes.

Collapse
 
eerk profile image
eerk

point 7 has been made easer : lastElement = array.at(-1)

Collapse
 
darkwiiplayer profile image
Info Comment hidden by post author - thread only accessible via permalink
𒎏Wii 🏳️‍⚧️

As always with these one-liners, whether one should "know" about them or actually use them are two entirely different questions.

To anyone looking at these and thinking about actually using them in real life, here's a couple things to consider:

Obfuscation

Taking a simple comparison like value === undefined (no, you don't need the typeof) and hiding it in a function does almost nothing to shorten the code, but hides what's actually going on, so anyone reading the code will have to jump to the definition instead of just reading what happens.

The same applies to redefining standard functions like const isArray = variable => Array.isArray(variable); (or, if you want to save typing, const {isArray} = Array. If you see Array.isArray(object) you know exactly what it does, or you can directly google it. If you see isArray(object), you'll have to jump to the definition first.

Pointless Code

Looking at an example like this:

const isObject = variable => typeof variable === 'object' && variable !== null;
Enter fullscreen mode Exit fullscreen mode

this could be simplified a fair bit by taking into consideration that something of type 'object' is only falsey if it is null, so this can be shortened to variable && typeof variable == "object"

Use === if it gives you or your linter peace of mind, but typeof always returns a string and the rhs is a constant, so it doesn't really change anything.

An extreme example: Compare flattenArray(array) to array.flat(). You type more by wrapping that in a function, and obfuscate what happens.

Pointless Complexity

The best way to do a thing is sometimes just the straight-forward solution.

If you want to get the last element of an array, just index it with its length + 1, and your code will be much easier to read. You'll also avoid allocating a new single-element array, which would the be up to your VM to optimise away in the best case or to garbage-collect in the worst.

Also, Array.prototype.at() is a thing; you can just use my_array.at(-1) and everyone can understand what it does as long as they know the standard functions well enough.

Other Nitpicks

hasProperty doesn't do what it says and can crash your code:

const p = document.createElement("p")
p.hasOwnProperty("lang") // false
"lang" in p // true

const o = Object.create(null)
"hasOwnProperty" in o // false
Reflect.hasOwnProperty.call(o, "hasOwnProperty") // false
o.hasOwnProperty("hasOwnProperty") // TypeError: o.hasOwnProperty is not a function
Enter fullscreen mode Exit fullscreen mode

The one about the palindrome would be a good starting point for an interview question: How could this function be optimised? The answer is, you only need to compare the first half to the latter half. Mirroring and comparing means every character gets compared twice.

Also that fibonacci function is half a fork bomb. Don't do fibonacci like that, you can literally google how to do it correctly (even recursively) in like 5 minutes.

Same for shuffling an array. That's gonna perform really poorly for large arrays.

Counting the occurrences of an element has a subtle but funny bug because it uses a plain Object without setting its prototype to null:

countOccurrences(["foo", "bar", "foo", "hasOwnProperty"])
// {foo: 2, bar: 1, hasOwnProperty: 'function hasOwnProperty() { [native code] }1'}
Enter fullscreen mode Exit fullscreen mode

For query parameters, just use URLSearchParams:

new URLSearchParams({foo: "foo", bar: "bar"}).toString()
// foo=foo&bar=bar
Enter fullscreen mode Exit fullscreen mode

And lastly, the most nitpicky one of them all: The longest word function will count words followed by a comma as one character longer than they are. Same for any special character, actually.


In conclusion, don't take any of these as examples for how to do things. Many of those would make for really good interview questions though. "What can you say about this function?" type stuff.

Collapse
 
jonrandy profile image
Info Comment hidden by post author - thread only accessible via permalink
Jon Randy 🎖️

AI generated/assisted posts should adhere to the guidelines for such content.

Some comments have been hidden by the post's author - find out more