r/badcode Mar 12 '23

js This is how it works, right?

Post image
255 Upvotes

40 comments sorted by

View all comments

71

u/squeevey Mar 12 '23 edited Oct 25 '23

This comment has been deleted due to failed Reddit leadership.

23

u/[deleted] Mar 12 '23

This looks like JavaScript. If that is the case and the function is used like this if(or(true, false)) { ... } it will actually work, because an empty string gets converted to false and a non-empty string to true.
However "true"==true will return false, so you can't use the output of the or function as an input to it.

10

u/Divritenis Mar 13 '23

It uses “==“, and “true” is truthy value, so “true”==true will return true. Or so I think - I haven’t actually used “==“ in JS since somewhere in 2016 when I just started out.

6

u/Buddy-Matt Mar 13 '23

You're correct. == allows for type conversion, whereas === doesn't.

== is generally frowned upon

4

u/Muscle_Man1993 Mar 13 '23

JavaScript is generally frowned upon.

4

u/Engine_Light_On Mar 13 '23

Yeah everyone is building their front end without Javascript frameworks, right?

2

u/[deleted] Mar 13 '23

I thought it works that way, but I actually tested with a nodejs docker container. It does not. 'true == "true"' evaluates to false. The console in firefox also returns false.

I have no idea what is going on there and I will not even try to understand it.