Mastering JavaScript: Top 50 One-Liners Every Developer Should Know
Know More :- https://codexdindia.blogspot.com/2024/02/top-50-one-liners-javas...
Some comments have been hidden by the post's author - find out more
For further actions, you may consider blocking this person and/or reporting abuse
So many of these are wrong. #5 will return true for
Dateand #13 actually takes more work to use as a one liner than it would just useString.includes().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:
You'll get many of the same examples, with the same mistakes.
point 7 has been made easer :
lastElement = array.at(-1)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 thetypeof) 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 seeArray.isArray(object)you know exactly what it does, or you can directly google it. If you seeisArray(object), you'll have to jump to the definition first.Pointless Code
Looking at an example like this:
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 tovariable && typeof variable == "object"Use
===if it gives you or your linter peace of mind, buttypeofalways returns a string and the rhs is a constant, so it doesn't really change anything.An extreme example: Compare
flattenArray(array)toarray.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 usemy_array.at(-1)and everyone can understand what it does as long as they know the standard functions well enough.Other Nitpicks
hasPropertydoesn't do what it says and can crash your code: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:For query parameters, just use
URLSearchParams: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.
AI generated/assisted posts should adhere to the guidelines for such content.