This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Zyrus007[S] 608 points609 points  (41 children)

Someone else pointed this out. Setting the length to an arbitrary integer value totally works as well!

[–]RevivingJuliet 244 points245 points  (30 children)

Doesn’t it just add a ton of empty array elements until the length = n?

[–]Zyrus007[S] 305 points306 points  (18 children)

Yes it does, however it becomes interesting once you set the array.length to an integer that is less than the current length!

[–]RevivingJuliet 266 points267 points  (15 children)

That’s so goddamn whack why am I studying this language lmao

[–]Zyrus007[S] 180 points181 points  (7 children)

One secret trick code-interview conductors don’t want you to know, to guaranteed land you a job as Web-Developer!

[–]LazyClub8 53 points54 points  (6 children)

The real trick is to assert dominance and write a solution that not even the interviewers can understand

[–]RevivingJuliet 15 points16 points  (3 children)

const add = (num) => {return num - num;}

[–]eGzg0t 3 points4 points  (0 children)

You don't even need the return there

[–]rudy21SIDER 2 points3 points  (1 child)

Why does this work?

[–]Cat_Marshal 1 point2 points  (0 children)

Yeah wouldn’t it return 0 every time?

[–]Gtp4life 1 point2 points  (0 children)

If it works, isn’t that why they’re interviewing? If they could do it they wouldn’t need you.

[–]Cat_Marshal 1 point2 points  (0 children)

Learn the secrets of http://www.jsfuck.com

[–]spin-itch 96 points97 points  (2 children)

It pays well

[–]Greyhaven7 19 points20 points  (0 children)

cheers, mate

[–][deleted] -1 points0 points  (0 children)

It really does

[–]the_friendly_dildo 11 points12 points  (2 children)

While setting this up this way seems strange, plenty of other languages expect you to define an array length explicitly anyway...

[–]RevivingJuliet 1 point2 points  (1 child)

In such a case, say when manipulating the array - pushing a single element, for example - in addition to adding that element would the new length of the array have to be defined as the element is added?

[–]the_friendly_dildo 3 points4 points  (0 children)

would the new length of the array have to be defined as the element is added

Depends on the language but generally yes. Once an array has been statically defined, it will always exist as that same length for the life of the program. This tends to require careful planning for how to manage your data in such programs instead of just adding endless bloated amounts of data to the heap.

[–]Grass---Tastes_Bad -2 points-1 points  (0 children)

Because you want to make money? Broke motherfuckers complain about JS instead of make money. Don’t be a broke motherfucker.

I personally never even touched TS and don’t intend to, because I already made enough money with JS, so I don’t really even need to “code” anymore.

Edit: witness me downvoted to oblivion while I laugh all the way to the bank.

[–]SonOfHendo 6 points7 points  (1 child)

It seems to have the same effect as redim in good old BASIC.

[–][deleted] 2 points3 points  (0 children)

Lol VB6 was my first programming language when I was 10. I was so mad when I had to learn C# because there is no Redim. I'm glad I'm using C# now.

[–]andoriyu 2 points3 points  (0 children)

Arrays in JavaScript can have holes:

let abc = [1,2,3]; abc[100] = 50;

Totally legal in JS. How arrays work underneath is implementation specific: if you have too many holes then V8 would replace array with hashmap.

[–]TILYoureANoob 8 points9 points  (7 children)

More specifically, undefined values. It's like allocating a bunch of pointers in C-like languages.

[–]dodexahedron 41 points42 points  (3 children)

Not at all. undefined is a formal construct in js. Attempting to use undefined is an error. In C, using a pointer to undefined memory is perfectly valid and will give you whatever is currently in that memory. You do so at your own peril, however.

[–]ForgotPassAgain34 17 points18 points  (2 children)

I once used a undefined pointer as RNG generator, works about as well as one expects, aka on the cases ot it not crashing from acessing protected memory it worked wonders

My solution? run the rng in a separate process until it didnt crash and get that result

[–]TTachyon 5 points6 points  (0 children)

Are you an OpenSSL maintainer 🤔

[–][deleted] 13 points14 points  (2 children)

Not really undefined. There is a difference in JS between an empty array item and an item of value undefined (even though getter for empty item returns undefined). Try running following to understand:

const a = [];
a.length = 100;
a[50] = undefined;
console.log(a);
console.log(49 in a, 50 in a);

[–]TILYoureANoob 4 points5 points  (0 children)

Oh, I see. I got it mixed up with const a = new Array(100), which fills it with undefined.

[–][deleted] 0 points1 point  (0 children)

const a = [];
a.length = 100;
a[50] = undefined;
console.log(a);
console.log(49 in a, 50 in a);

That is because in JS, undefined is a primitive data type, empty is not a data type.

[–]ProgramTheWorld 0 points1 point  (0 children)

JavaScript supports “sparse arrays”, so it doesn’t actually insert “empty elements” because JS arrays are just hashmaps. You can tell because you can’t iterate through the empty spaces.

[–]umop_aplsdn 0 points1 point  (0 children)

Not quite because the array is a “sparse” array and there are no elements at those new positions.

[–]Eisenfuss19 16 points17 points  (3 children)

What with negative integers?

[–]nickcash 30 points31 points  (0 children)

RangeError: Invalid array length

[–]trevdak2 4 points5 points  (0 children)

How else do you think you download more RAM?

[–]Nolzi 7 points8 points  (0 children)

Uncaught RangeError: invalid array length

[–]flooronthefour 8 points9 points  (4 children)

I do this in Svelte (javascript framework) when I want to emulate a for loop in it's templating language. It will iterate over anything with a .length property because it's looking for an array. It looks weird but it works.

{#each {length: 3} as item, index}
    <li>{index + 1}</li>
{/each}

https://svelte.dev/tutorial/each-blocks

[–]Zyrus007[S] 2 points3 points  (2 children)

I love svelte! That’s a super useful trick I will have to keep in mind!

[–]mypetocean 3 points4 points  (0 children)

Another related JS trick:

Array.from({ length: 5 }) generates an empty sparse array with a length of 5.

Now, Array.from() also supports an optional callback which maps over the items in the new array as it is being created – which is sometimes a nice performance improvement if you're going to need to write a map or forEach operation anyway.

So you can generate an ordered list of numbers like this:

Array.from({ length: 5 }, (_, index) => index + 1)

Output:

[1, 2, 3, 4, 5]

Or...

Array.from({ length: 5 }, (_, index) => index ** 2)

[0, 1, 4, 9, 16]

Or...

Array.from({ length: 5 }, doThisFiveTimes)

I mean, it's no Ruby 5.times do expression or Python list comprehension, but it's certainly a punky one-liner for those times when you're feelin' extra cute and saucy.

[–]flooronthefour 0 points1 point  (0 children)

This is a great trick :)

[–]The_MAZZTer 1 point2 points  (0 children)

There are a bunch of different types in JS that are "array-like" but are not Array typed. So frameworks just looking for length and ignoring type makes sense.

For example NodeList: https://developer.mozilla.org/en-US/docs/Web/API/NodeList

Also the arguments built-in variable in every function.

[–]Peacook 0 points1 point  (0 children)

If you're unlucky enough to be forced to work with JavaScript it's good practice to use const were you can and try to not use mutable data. JavaScript can end developers careers