5 JavaScript Tips You Probably Don't Know

  Рет қаралды 9,328

Andrew Burgess

Andrew Burgess

Күн бұрын

Пікірлер: 46
@fagnersales532
@fagnersales532 Жыл бұрын
Wouldn't it be possible to create a 2d Array using `Array.from({ length: 5 }, () => [])`?
@andrew-burgess
@andrew-burgess Жыл бұрын
oh, love this! really great way to create a 2D array!
@AppleGameification
@AppleGameification Жыл бұрын
That's so amazingly grotesque
@vanzed7539
@vanzed7539 Жыл бұрын
Array.from( Array(3), (v,i)=>Array.from( Array(3), (k, n)=>({x: i, y: n}) ) ); 3 * 3 array of point objects.
@diegodoumecq5144
@diegodoumecq5144 Жыл бұрын
Damn, came here to comment this exact same thing
@tantalus_complex
@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.
@dimitro.cardellini
@dimitro.cardellini Жыл бұрын
2d arrays: const a = Array.from({ length: 5 }, () => []); range: const rangle = (start: number, end: number) => { const [length, step] = end > start ? [end - start + 1, 1] : [start - end + 1, -1]; return Array.from({ length }, (_, index) => start + index * step); }
@Ma1ne2
@Ma1ne2 Жыл бұрын
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
@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
@mluevanos Жыл бұрын
I like creating array like this: Array.from({length: 10}, (_, i) => i)
@SomewhatLazy
@SomewhatLazy Жыл бұрын
5 JavaScript tips... in TypeScript. Great job.
@mateuszgroth7480
@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
@Jambajakumba Жыл бұрын
Wow really cool video. I didn't know about a bunch of these additions Andrew. Thanks!
@MrREALball
@MrREALball Жыл бұрын
Didnt know you could do this type of thing with bind. Would use it in my code now, probably.
@parlor3115
@parlor3115 Жыл бұрын
Better to just use higher order functions
@MrREALball
@MrREALball Жыл бұрын
@@parlor3115 im not sure, but I think garbage collector would disagree
@parlor3115
@parlor3115 Жыл бұрын
@@MrREALball What do you think bind does? It creates a new function as well.
@todd_matheson
@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
@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
@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
@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
@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
@fagnersales532 Жыл бұрын
Can you talk about error handling?
@andrew-burgess
@andrew-burgess Жыл бұрын
yeah, that'd be a good topic! Anything specific you're interested in hearing about?
@AppleGameification
@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
@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
@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
@JaimeOlmo Жыл бұрын
Map and Set, I would love a video about this topic.
@PickledHam
@PickledHam Жыл бұрын
just wondering when you were a teen if you are from winchester, tn . you look familiar
@andrew-burgess
@andrew-burgess Жыл бұрын
🇨🇦 🙂
@majorhumbert676
@majorhumbert676 Жыл бұрын
If you work with immutable data, it won't be a problem to instantiate arrays like this.
@DontFollowZim
@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
@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
@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
@lukem121 Жыл бұрын
More 🎉
@andrew-burgess
@andrew-burgess Жыл бұрын
For sure! Anything specifically?
@lukem121
@lukem121 Жыл бұрын
@@andrew-burgess More tips like this. Underused JS/TS features. Computation time savers. Best JS/TS/React practices
@ContortionistIX
@ContortionistIX Жыл бұрын
good
@DanelonNicolas
@DanelonNicolas Жыл бұрын
nice!!
@montebont
@montebont 11 ай бұрын
The title is misleading, The video is about Typescript and not about Javascript.
@mzerone-g6m
@mzerone-g6m Жыл бұрын
Just stop doing functional and mutate your state
@aidemalo
@aidemalo Жыл бұрын
>5 JavaScript Tips Proceeds with TS, dislike
@lucasa8710
@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
Are your TypeScript Unions broken? | Advanced TypeScript
7:36
Andrew Burgess
Рет қаралды 8 М.
What are JavaScript Generators and Iterators?
10:28
Andrew Burgess
Рет қаралды 6 М.
Cheerleader Transformation That Left Everyone Speechless! #shorts
00:27
Fabiosa Best Lifehacks
Рет қаралды 14 МЛН
Леон киллер и Оля Полякова 😹
00:42
Канал Смеха
Рет қаралды 4,3 МЛН
The Best Band 😅 #toshleh #viralshort
00:11
Toshleh
Рет қаралды 20 МЛН
5 JavaScript Concepts You HAVE TO KNOW
9:38
James Q Quick
Рет қаралды 1,4 МЛН
7 MUST KNOW JavaScript Tips and Tricks
8:58
dcode
Рет қаралды 21 М.
An array of ways ... to create arrays (JS Basics)
11:04
Andrew Burgess
Рет қаралды 1,9 М.
Vim Tips I Wish I Knew Earlier
23:00
Sebastian Daschner
Рет қаралды 81 М.
any vs unknown vs never: TypeScript demystified
8:01
Andrew Burgess
Рет қаралды 25 М.
My Favorite TypeScript Tips and Tricks
10:21
Lachlan Miller
Рет қаралды 7 М.
10 Math Concepts for Programmers
9:32
Fireship
Рет қаралды 1,9 МЛН
old TypeScript syntax I just discovered
8:54
Andrew Burgess
Рет қаралды 4,1 М.
Managing Promise Concurrency in JavaScript
12:44
Andrew Burgess
Рет қаралды 8 М.
will i never understand this? unknown.
12:05
Andrew Burgess
Рет қаралды 3,9 М.
Cheerleader Transformation That Left Everyone Speechless! #shorts
00:27
Fabiosa Best Lifehacks
Рет қаралды 14 МЛН