Javascript humor thread

Share your funny or weird javascripts

typeof NaN

Attached: thanks_javascript.png (484x549, 162K)

Other urls found in this thread:

rextester.com/l/js_online_compiler
playcode.io/
hackage.haskell.org/package/base-4.12.0.0/docs/src/Data.Functor.Utils.html#Max
twitter.com/NSFWRedditImage

const [a, b] = [1, 2]
const [c, d] = [3, 4]
[a, b] = [b, a]
console.log(a, b, c, d)

my favorite

someone fucking contribute you fuckers usually don't have a problem with shitting on js

Attached: cpmjaci6rol21.jpg (274x301, 18K)

is this some kind of easter egg? I refuse to believe a language can be so broken

nah it's just one of those exceptions where the semicolon is not optional

Typeof NaN makes me laugh every single time. I don't know why but it just never stops being funny. It's so fucking dumb.

Attached: best-js-meme-to-date-2.png (500x760, 655K)

absolute classic
[1,2,3,4,5,6,7,8,9,10].sort()

It expect a function for sorting, default is alphabetical i think

Javascript is kinda comfy desu

He is retarded, ignore him

Attached: 1564975356375.jpg (774x960, 92K)

Attached: 1563853139190.jpg (1024x1343, 232K)

Attached: 1564691520833.jpg (999x277, 61K)

>math max and min
ok HOL UP
How the fuck can max be negative infinity. Literally every number is greater than math max

Attached: 1563218671536.jpg (506x662, 56K)

Attached: 1560756315769.jpg (600x600, 57K)

>1 2 2 1
WHAT THE FUCKING FUCK?????

Attached: 1561331875911.jpg (705x527, 247K)

KEK

it's a comparison function with no arguments given to it, not a function to return the maximum or minimum of all possible numbers.

Obviously you'd want the default argument of Math.max to be -Infinity so anything you actually pass to it is greater than that.

What the fuck why? If I give it one number does it actually compare it against negative infinity?

retard
> const [a, b] = [1, 2]
> const [c, d] = [3, 4]
> [a, b] = [b, a]
Thrown:
TypeError: Assignment to constant variable.
> console.log(a, b, c, d)
1 2 3 4

Printed 1 2 2 1 on my javascript

Attached: 1542711425096.png (708x470, 462K)

Can we do better than js tho? I mean, if we restrict to intepreted bytecode

impossible

Try the code here.
Just use print instead of console.log
rextester.com/l/js_online_compiler

top fucking kek

Attached: 2019.08.07-17.52.44.264004982.png (196x46, 5K)

I think it's because you did it in the console.
This is the code that's actually being executed if it was in a JS file.

const [a, b] = [1, 2]
const [c, d] = [3, 4][a, b] = [b, a]
console.log(a, b, c, d)
1 2 2 1

i dont get it
how's it possible to re-assign to a const variable?
> [e,f] = [1,2]
> [g,h] = [3,4]
> [e,f] = [f,e]
> console.log(e,f,g,h)
2 1 3 4


why does it print 1221?

Yeah the first 6 operations make perfect sense, not gonna defend the others though.

that doesn't explain the 1 2 2 1 part

I dont use javascript but I suspect its because javascript is garbage.

On that note, Elm is a functional language that compiles to javascript.

i just posted an example that works as intended

>comparing float values directly
The retard here is you.

did you have a point there big brain?

did you?

yes javascript is likely garbage

for you

Doesn't work idiot.

@72228730
>1 2 2 1

i know, but try on this website for example

playcode.io/

So [3, 4][a, b] results in undefined. And for god knows what reason, when you have the following:
const x = undefined = 2

x is assigned 2. So in the original case, it ends up being [c, d] = [b, a]

damn

You have 5 seconds to name why JavaScript should be used outside of button events.

Pro-tip: you can't

>(!+[]+[]+![]).length
WHAT the fuck LMAO

What the fuck does ! + even do. It's like a possessed version of ! that is very similar but randomly gives the opposite. What the fuckkkkkk

Attached: 1563083822143.png (640x600, 614K)

Attached: banana.png (304x83, 3K)

I went like "wtf" but then remembered that, back when it was cool to omit semicolons, we put semicolons at the beginning of lines starting with a '['.

Because the const variable contains an array. Arrays aren't primitive types, they're special objects. The numbers in the array are properties of the object. It's a classic case of passing all objects by reference and primitives by value - but the language never had "const" designed in from the start, so completely unlike e.g. C++, the const only ever applies to the direct value stored in a variable. Which in the case of an object/array, is the reference to the object/array data, not the actual data itself - so the latter is perfectly mutable. Enjoy.

>What the fuck does ! + even do
! negates and [] is truthy, so ![] is false
unary + converts the argument to a number, so +[] is 0. 0 is falsey, so !+[] is !0 is true.
binary + sees a true on the left and [] on the right, and converts both of them to their nearest compatible type, which is string. true becomes "true", and [] becomes "", and "true"+"" becomes "true".
The last ![] evaluates to false (see above). "true" + false again converts both to strings before concatenating, and we end up with "truefalse", the length of which is 9.

Yeah but does + evaluate its operands right to left consistently? Because ! doesn't look like a valid operand, so if you had "! + 0" => "! (+0)" => "!0" that makes sense, but if it ever went "! + 0" => "(! +) 0" then I don't see how it would work.

You already got it wrong in the very first sentence.

Explain

This is a wrong explanation. It just has to do with missing semicolons.

>. It just has to do with missing semicolons.
this

adding it makes the code throw an error
const [a, b] = [1, 2];
const [c, d] = [3, 4];
[a, b] = [b, a] ;
console.log(a, b, c, d);

I'm not sure what you're trying to get at. "! + 0" evaluates the same as "!(+0)", using the unary + operator. It will always evaluate that way and never like "(!+)0" which would indeed be grammatically invalid.
I think you're thinking of this from the perspective of the + operator "grabbing" its left and right side, but what actually happens is the ! operator "grabs" the next valid expression to its right, which in this case is "+0"

it's like a droid that hasn't had a memory wipe

>ywncokbf

Reminder that despite all this crap Javascript devs are seriously high in demand and make good bucks.

That’s the IEEE 754 floating point spec

nice

The type should be "float" or something, then, not "number".

Javascript's Number type is exclusively double precision IEEE 754 floating point. That's what it means.

the worst thing is that people are pushing for this shit in the backend

Time for a short CS/math lesson.
JavaScript's max and min functions are variadic, meaning they take any number of arguments. How do you define such a function? You can't define it for each possible number of arguments, because you'd never finish. What you can do is use recursion:
// 2 parameters
max(x, y) = if x >= y then x else y
// n + 1 parameters
max(x, ...) = max(x, max(...))

But with that definition, we still only know how to find the maximum of 2 or more arguments. Obviously, the maximum of 1 argument is the argument itself.
max(x) = x

But that overlaps with the definition for n + 1 arguments! Now we have to solve this equation:
x = max(x, max())

max() must obviously be something that is at least as small as any other number, or else this equation would not hold. Negative infinity (assuming the number line in question is the extended reals, which floating point numbers approximate) works just fine. Hence max() = -inf. You can repeat this exercise with min to find that min() = inf.
More abstractly, max(x, y), max(), min(x, y) and min() form a bounded lattice, which automatically gives you operations for the maximum and minimum element of any finite set (variadic functions take lists, but we additionally have that max(x, x) = x and max(x, y) = max(y, x)).

Just don't confuse "the maximum of an empty set of numbers" with "the greatest number" and you'll be good.

this is not a problem with superior haskell
hackage.haskell.org/package/base-4.12.0.0/docs/src/Data.Functor.Utils.html#Max

>typeof Not a Number
>"number"
I don't know why but I lost my shit

Attached: 1559767735331.png (496x736, 292K)

You don't have to use recursion, you can just loop. E.g. python
def max(*args):
if not args: return None
m = args[0]
for x in args[1:]:
if x>m:
m = x
return m

But yeah, obviously returning -infinity as the max of zero args is perfectly reasonable, and even if recursion isn't the real implementation, it's reasonable for max(A+B)=max(max(A),max(B)) for any lists A and B, even empty lists.

i demmand a explanation

>+ 'a'
>NaN

i cant tell if this is genious or some fractal madman puzzle solution

It's funny how all this makes sense why this happens, instead or being a bumbling retard going "duuuurrrr". I feel enlightened.

very cool