Generative AI has blessed the world with many listicles, some helpful, some less so. But with all that focus on having AI do actual work, we are missing out on the fun side: Making AI do dumb stuff. So for this post, I asked ChatGPT to give me 20 JavaScript one-liners that don't actually work.
This post has been copied as-is from ChatGPT with two changes:
- I added some notes in between using quote blocks so they can be easily identified
- To prevent markdown resetting all the counters to 1, I had to add a zero-width space between the numbers and the
.
; otherwise all of them would have said1.
Title: Top 20 JavaScript One-Liners That Don't Actually Work
JavaScript, with its versatility and simplicity, often lends itself to concise, one-liner solutions to common programming problems. However, not all one-liners are created equal. In this blog post, we'll explore some JavaScript one-liners that may appear to work at first glance but actually fail under closer scrutiny. Let's dive in and unravel the mysteries of these seemingly innocent lines of code.
1. The False Truth:
const truth = true;
if (truth = false) console.log("This will never be executed");
A common mistake of using assignment (=
) instead of equality (==
or ===
). The condition will always be false, leading to confusion.
Human Note: Starting off strong with a two-liner. I guess the first line is just the preamble for the actual example, but I still find it funny.
2. Infinite Loop Illusion:
for (let i = 0; i != 10; i--) console.log(i);
Despite the intent to count down from 0 to 9, i
will decrease infinitely due to i--
, never reaching the loop exit condition.
Human Note: There's no deeper wisdom to be found in this one, it literally just is a broken for loop. Try this with a
char
in C and it might actually work.
3. Truthy Falseness:
if ([]) console.log("This will be executed");
An empty array is truthy in JavaScript, contrary to some expectations.
Human Note: I had to double-check this one and yea, it's actually true. See? I already learned something. And within 10 minutes, I will already have forgotten it again.
4. The NaN Trap:
if (NaN === NaN) console.log("This will never be executed");
Comparing NaN with anything, including itself, always returns false. Use isNaN()
for NaN checks.
Human Note: Quality advise. But if you wanna be extra fancy, you can compare a value to itself to check if it's NaN. Your coworkers will thank you.
5. Function Overload:
const foo = () => console.log("foo");
const bar = () => console.log("bar");
const baz = () => console.log("baz");
const qux = () => console.log("qux");
const func = foo() && bar() && baz() && qux();
All functions will be called regardless of their return values. To stop on the first false value, use the logical OR (||
) operator.
Human Note: I tested this in the browser and looked it up on mdn. As far as I can tell, ChatGPT is straight up bullshitting us here; only the first function will be called.
6. The Lost Context:
const obj = {
value: 42,
getValue: () => this.value
};
console.log(obj.getValue());
Arrow functions don't have their own this
context, leading to undefined
.
Human Note: This is misleading. Arrow-functions don't set their own
this
context when called, but they save the surroundingthis
. So if this object is defined wherever athis
exists, it will refer to thatthis
. Sometiems this is exactly what the programmer wants. In fact, this is partly the point of arrow functions.
7. The Illusion of Privacy:
const mySecrets = {
password: "12345",
getSecrets: () => this.password
};
console.log(mySecrets.getSecrets());
Similar to the previous example, arrow functions inside objects don't provide privacy as expected.
Human Note: I legit have no clue what ChatGPT is even trying to tell us here. If you care about privacy, you will be excited about the sponsor of this video... Wait, wrong script.
8. Looping Headache:
const arr = [1, 2, 3];
for (var i = 0; i < arr.length; i++) {
setTimeout(() => console.log(i), 1000);
}
The closure captures the reference to i
, which is hoisted and becomes 3 by the time the callback executes.
Human Note: Yes, that's what
var
does. This is why you uselet
instead unless you specifically wantvar
.
9. Promise Trap:
Promise.resolve('foo').then(console.log);
console.log('bar');
then
expects a function as an argument. console.log
gets called immediately, not when the promise resolves.
Human Note: I have no clue what the toaster is trying to tell me. Maybe it's time for the airlock.
10. String Reversal Sorrow:
```javascript
const reverseString = str => str.split('').reverse().join('');
console.log(reverseString('hello'));
```
This approach doesn't work with Unicode characters or emoji.
Human Note: Ah yes, a true classic. Literally every "top 10 one-liners" listicle here on dev has this code, and literally every one of them has at least a million comments pointing out the unicode issue.
... (Continuing with more examples)
Human Note: Note how the title says 20, but there's actually only 10. This "AI" can't even count past 20 and people are trusting whatever it spits out without even double-checking.
In conclusion, while JavaScript one-liners can be elegant and efficient, they often hide complexities that may lead to unexpected behavior. It's crucial to thoroughly test and understand each line of code, especially when condensed into a single line. Remember, readability and clarity should never be sacrificed for brevity.
Human Note: And hopefully, I've been able to show that this is even more important when relying on code you get from some random AI chat.
So there you have it. 20 10 javascript one-liners that don't actually work. Except some do. I hope this silly little post has been at least somewhat amusing, and above all else, that I've been able to show why AI generated content shouldn't be trusted blindly. More importantly, I hope I've made clear how AI generated posts like this one can contain both obvious and very subtle mistakes, and posting them on the internet without even fact-checking them can seriously mess with people who are honestly trying to learn JavaScript.
Top comments (13)
This is probably the best AI-aided listicle post ever! Insightful on different levels and amusing as well :-)
Haha, this is very entertaining and such a cool idea. Lovin' the human notes! Well done! 🙌
Hilarious! Thanks for this great read. 😂🙌
This was really fun to read! 😂 And hey, not only it shows why not to trust blindly ChatGPT, but it also reminds us of why not to trust JavaScript at a glance (or at all if you ask me 😂)!
Thank you so much!!
This is even better than the unironical listicle about JS1liners!
Thanks for sharing this
very informative
I appreciate the feedback, but I think you've missed the point of the post 😅
Yep. The point of it clearly went over your head 🤭
Bruh...