Wouldn't it be possible to create a 2d Array using `Array.from({ length: 5 }, () => [])`?
@andrew-burgess Жыл бұрын
oh, love this! really great way to create a 2D array!
@AppleGameification Жыл бұрын
That's so amazingly grotesque
@vanzed7539 Жыл бұрын
Array.from( Array(3), (v,i)=>Array.from( Array(3), (k, n)=>({x: i, y: n}) ) ); 3 * 3 array of point objects.
@diegodoumecq5144 Жыл бұрын
Damn, came here to comment this exact same thing
@tantalus_complex Жыл бұрын
It's actually more performant than the new Array/fill()/map() approach, as well. That approach loops over the allocated array both with the fill() and the map() separately. The Array.from() approach, however, fills the array as it runs the provided map function - looping once. Not bad. If you want to do more than a one-liner in that callback function, though, I'd recommend naming it and passing it in by name for readability.
I am really loving these JavaScript videos. Today everything is about TypeScript and while I love using and learning about it, it sometimes doesn't help you with solving a problem. You need to understand the underlying language (JavaScript) and these videos are helping to learn about some of the more modern features and APIs that JavaScript offers. Then in conjunction with TypeScript, you get superpowers.
@dechobarca Жыл бұрын
Oh damn, #4 is really tricky that it behaves the way it does to be honest. I mean maybe not according to the language specification, but I can see a lot of people accidentally slipping before/unless they test their code, myself included. Thanks for the video, really cool one.
@mluevanos Жыл бұрын
I like creating array like this: Array.from({length: 10}, (_, i) => i)
@SomewhatLazy Жыл бұрын
5 JavaScript tips... in TypeScript. Great job.
@mateuszgroth7480 Жыл бұрын
The array fill caught me off guard once during an interview for a job. I had to do some kind of algorithm, which I actually coded the right way, but I initialized the array like you showed and it screw me over. I learned it the 'hard' way unfortunately ;D
@Jambajakumba Жыл бұрын
Wow really cool video. I didn't know about a bunch of these additions Andrew. Thanks!
@MrREALball Жыл бұрын
Didnt know you could do this type of thing with bind. Would use it in my code now, probably.
@parlor3115 Жыл бұрын
Better to just use higher order functions
@MrREALball Жыл бұрын
@@parlor3115 im not sure, but I think garbage collector would disagree
@parlor3115 Жыл бұрын
@@MrREALball What do you think bind does? It creates a new function as well.
@todd_matheson Жыл бұрын
It's much more performant and better to use `Array.from({ length: 5 }), () => [])`. It's more concise and it only iterates once over the array which is much better for larger arrays. Also, the second argument to Array.from, the initializer function takes 2 arguments, the seconds being the index, so that comes in super handy sometimes too.
@Luxcium Жыл бұрын
I am sorry I do not understand the *declare* keyword purpose on line 2 when you explain the _NCO_ @ 2:00 ... or in any other cases where you have been using it in the examples...
@andrew-burgess Жыл бұрын
It's a TS thing, a way to declare the TYPE of a variable or function without initializing it or giving it a value. Not really useful in production, but great for testing how types interact with each other.
@joostschuur Жыл бұрын
Completely unrelated, but what is going on with that 'fi' font ligature? Curious that that isn't monospaced but everything else is?
@andrew-burgess Жыл бұрын
I know, right? That’s just the TS playground online. I’ve noticed the same thing on multiple machines, so I don’t think it’s anything weird on my side. But yeah, it’s always the “fi”.
@fagnersales532 Жыл бұрын
Can you talk about error handling?
@andrew-burgess Жыл бұрын
yeah, that'd be a good topic! Anything specific you're interested in hearing about?
@AppleGameification Жыл бұрын
One thing I struggle with is handling complex error handling logic. The lazy thing to do is to catch the error and just return a null or a generic error, no matter what went wrong. But sometimes it's useful for the consumer of your function to know some detail of what went wrong. I've heard some OTHER people say that they don't like to use exceptions or errors as a messaging system between application layers or between services. What's your take on all of this?
@FunctionGermany Жыл бұрын
@@andrew-burgess creating/throwing/catching different or custom error types, or how to do error handling in a functional codebase (since error handling in JS seems very OOP)
@mmmikem Жыл бұрын
@@andrew-burgess I think "cause" is not spoken about enough. I've seen some gnarly error handling when communicating with external services and dBs in the same service.
@JaimeOlmo Жыл бұрын
Map and Set, I would love a video about this topic.
@PickledHam Жыл бұрын
just wondering when you were a teen if you are from winchester, tn . you look familiar
@andrew-burgess Жыл бұрын
🇨🇦 🙂
@majorhumbert676 Жыл бұрын
If you work with immutable data, it won't be a problem to instantiate arrays like this.
@DontFollowZim Жыл бұрын
Only the 2d array tip was new for me, but also probably the least useful since I can't remember a time where I ever created a set size 2d array without looping over data that needs to get injected into that array anyway...
@thematrix185 Жыл бұрын
Rather than doing new Array(x).fill().map(), I think [...new Array(x)].map() is nicer to read. Don't know the exact performance implications of spread vs fill but I only ever use it for small arrays so I'm sure it's negligible
@FunctionGermany Жыл бұрын
i see. by spreading, the "empty" places are converted to undefined, making them map-able. i prefer .fill() because it's more expressive, less guess-work.
@lukem121 Жыл бұрын
More 🎉
@andrew-burgess Жыл бұрын
For sure! Anything specifically?
@lukem121 Жыл бұрын
@@andrew-burgess More tips like this. Underused JS/TS features. Computation time savers. Best JS/TS/React practices
@ContortionistIX Жыл бұрын
good
@DanelonNicolas Жыл бұрын
nice!!
@montebont11 ай бұрын
The title is misleading, The video is about Typescript and not about Javascript.
@mzerone-g6m Жыл бұрын
Just stop doing functional and mutate your state
@aidemalo Жыл бұрын
>5 JavaScript Tips Proceeds with TS, dislike
@lucasa8710 Жыл бұрын
5 javascript tips for productivity 1. Don't use JavaScript 2. Don't use JavaScript 3. Don't use JavaScript 4. Don't use JavaScript 5. Don't use JavaScript By following these tips you'll be able to enjoy more your life as a programmer