Being able to write awful code with useful syntax doesn't make JS a bad language though. Yes I know why it gets so much bad reputation, but if we throw away years of baked in legacy it's really not that bad.
Same goes for many other languages and their shortcomings:
C++:
const correctness is desireable nowadays, but requires const everywhere (instead of it being the default
manual pointer handling is typically not necessary anymore, but unlike e.g. Rust they are easily accessible and part of the "fundamentals"
ownership modeling (with smart pointers) is important, but also just a "convention" (i.e. the compiler doesn't support you)
having to deal with headers. It's understandable why they are there, and why they are still there, but I feel like this is something that could be more automated
Macros
Java:
type erasure (forgetting the generic type arguments at runtime), made more difficult if you have generic and non-generic versions of the same class
primitive types not being part of the remaining type hierarchy and thus not an option for generics (you have to use their wrappers)
Optional<T> has some nice things about it, but there might have been better approaches (see C#, Kotlin, Dart, ...)
the Stream API is fine, as long as the predefined metgods are enough. Without extension functions/extension methods, you can't extend it yourself
C#:
even though nullabillity handling is better than in Java, Nullable<T> (like int?) doesn't behave the same as nullable reference types (e.g. string?): even after a null check you have to use .Value
...You're probably still better off with headers, unless you start a new project and design it to use modules from the ground up, since they require actual design thought and can't just be slotted in like header copypasta.
It does make it a bad language. This is a language design problem.
JS takes the philosophy of "I must never complain about what the developer is asking me to do. It is better to take instructions that make no sense and do something than it is to error"
That results in things like the OP. It would be better for everyone if it just errored, because who wants to do that?
Once you've worked in a language with property type safety, it becomes very clear that it's a better approach than whatever-the-fuck-you-want typing.
Thats a hot take if I’ve ever seen one: good syntax doesn’t make a bad language good. What makes JavaScript bad is the amount of implicit heavy lifting the language does instead of simply erroring (or warning) out. There are (hyperbolically) 501 different ways to do the same thing in JavaScript, of which 490 are nonsense, and the fact that the language allows this is a testament to its shortcoming.
Programmers are only ever so good, if the language enables them to write balls of mud instead of warning them about this syntax then that’s a failure on the language. Eg: warning: implicit array destructuring, or warning: implicit assignment in parameters. For default values it shouldnt be encased in an object, imo, or done like `(a = {b: val})`. If the language warned about these things then that wouldn’t be this bad. Good syntax is not a redemption for having too permissive practices.
JavaScript is an accessible language that may in turn teach bad habits, and removing the legacy doesn’t change the fact that JavaScript is like a literature student, saying "now what did the programmer mean and how do we make it work?" (Declarative languages only do former)
I think it was fine as a webpage scripting language, but now it's being used everywhere. It certainly seems like it was designed to make every effort to keep going, no matter how little sense the code makes.
For those wondering Javascript allows you to define default values, and the code is taking “advantage” of that twice.
The code uses array destructuring since javascript will treat a string as an array of single character strings when you do array destructuring. So if you pass an empty string, there is no first object to destructure, and so the default object { a: true } is used.
If the string is non-empty then the first character will be used to try to destructure the object, however strings don’t have a property a to destructure, so the default a = false is used.
It’s far more readable to extract multiple fields from a struct that way, especially if they’re nested. Better than repeating the entire path to some nested struct multiple times. Plus it mirrors the struct construction syntax so it can be easier to read at a glance.
in c/c++ I could just avoid the repetition by making references to the nests of the structs I want to access many times. Is that basically the c's way of destructuring?
Not really, that’s a separate thing. It’s more about accessing multiple fields of one struct, whether or not that struct is nested inside something else
Here if the string is not empty, arr[0] will be defined as the first character (and saved in firstChar). The character won't have an a attribute, so firstChar.a is undefined, and the function returns false.
If the string is empty, its first character is undefined, and firstChar will be set to { a: true }. The the function will return its a attribute, which is, of course, true.
Proving that this code is essentially equivalent to the screenshot is left as an excercise to the reader : )
First the string is destructured into an array with 1 element. If the string is empty, the first defsult value of { a: true } is used, i.e. an object with a set to true, with a also being a local variable. If the string is not empty, the first entry (first character as string) is then attempted to be destructured into { a = false}, i.e. an object with a property a. Since strings don't have an a property, the default value of false is used. I assume that if instead of a something like length was used, the return type would be int|false (if you understand my TypeScript).
function isStringEmpty(arg) {
const iterator = arg[Symbol.iterator]();
const m = iterator.next();
const item = (m.done ? undefined : m.value) ?? { a: true };
// this can still produce false results if the passed `arg` has a first item
// with a property `a` that is not `null`/`undefined`
const a = item.a ?? false;
return a;
}
This will crash if arg is null/undefined or if it does not implement Symbol.iterator or if arg[Symbol.iterator]() does not return an object or if that object does not implement .next() or if that method does not return an object.
Yes that may seem nitpicky, until you see some of the legacy code that's out there.
445
u/Aaxper May 27 '26 edited May 27 '26 •
isStringEmpty([ ... ])tries to destructure the string as an array...part of that, it only matches on the first element (the first character){ a = false }tries to destructure the first characteraproperty, which doesnt exist, so it defaults to settingatofalseaproperty, it defaults to{ a: true }, which setsato trueaisfalse,elseais trueI think that's correct
225
u/turtle_mekb May 27 '26
what the fuck
129
u/eloel- May 27 '26
Gross. Also can error or return true or false for non-string answers, which makes it doubly gross.
116
u/Denommus May 27 '26
Maybe if I understood Javascript destructuring syntax that would make sense to me. But since I don't, this looks awful.
21
u/EatingSolidBricks May 27 '26
Ok so
Empty string destrucutres to nothing? So a is true?
Non empty string destrucutres to a truthy value so false?
Wtf is this shit
11
u/nosam56 May 27 '26
adding
String.prototype.aor whatever the fuck17
u/itz_hez May 27 '26
Thanks, this solves a problem I am currently having.
8
u/iamdatmonkey May 27 '26 edited May 27 '26
This is what this construct comes down to.
This will crash if arg is
null/undefinedor if it does not implement
Symbol.iteratoror if
arg[Symbol.iterator]()does not return an objector if that object does not implement
.next()or if that method does not return an object.
Yes that may seem nitpicky, until you see some of the legacy code that's out there.
4
u/serg06 May 27 '26
That's why no self-respecting developer uses plain JS anymore.
3
u/meowmeowwarrior May 27 '26
Not sure if I should be appalled at the author writing it or the language for making it possible
2
u/Icy_Curve711 May 27 '26
I'm ok with this kind of destructuring, but truthiness makes it utterly illegible.
2
u/CiranoAST May 29 '26
JavaScript = pure evil
3
u/fletku_mato May 27 '26
JavaScript was a mistake.
3
u/Thenderick May 27 '26
Why not just
str === ""?1
u/_just_mel_ May 27 '26
The JavaScript devs YEARN for the prolog
1
u/Ordinary_Yam1866 [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” May 28 '26
Unholy! UNHOLY!!!
1
u/No-Wishbone-3171 May 28 '26
Why?
1
u/DefinitionPhysical46 May 28 '26
My head still hurts.
4
u/Chemical_Present6069 May 27 '26
interview fantasy question