Another 5 Must Know JavaScript Features That Almost Nobody Knows

preview_player
Показать описание
🚨 IMPORTANT:

JavaScript is a vast language with tons of features and it is impossible to know them all. In this video I will be covering yet another 5 features in JavaScript that nobody knows but are incredibly useful.

📚 Materials/References:

🌎 Find Me Here:

⏱️ Timestamps:

00:00 - Introduction
00:57 - Loop Labels
09:18 - Map
12:32 - Set
13:56 - Binary Math
17:39 - Debugging Tips

#JSTips #WDS #Top5JS
Рекомендации по теме
Комментарии
Автор

Been working with JavaScript on and off since 1998, and I still find tons of values in your videos. Kudos, Kyle, on these excellent tutorials—you’re a real community asset

prometheas
Автор

So in case anyone wondered how decimal numbers can be represented in memory. If we push a 1 to the left, we double its value. If we push it to the right, it's going to be halved. So let's do it, starting with 1:
decimal -> binary
1 -> 1
.1 -> 0.5
.01 -> 0.25
.001 -> 0.125
Need 0.75 in decimal? That's 0.5 + 0.25 so it would be .11. Now let's look at the famous 0.1 + 0.2 =

0.1 (dec) is smaller than 0.125 (1/8) so we need the next 2^-x power, 1/16, that's 0.0625 (.0001 binary). We're left with 0.0375, which is a bit larger than 1/32 (0.03125 dec, .00001 binary). 1/16 + 1/32 is therefore 0.09375 in decimal (.00011 binary). We would need 0.00625 more to 0.1, we're getting closer to 0.1, but in this case, since 0.1 can't be a sum of 2^-x powers, you will reach the maximum number of bits threshold so your value gets truncated. Imagine if he had only 4 bits, the best binary representation for 0.1 would have been 0.0001, which is only 0.0625!

0.2 (dec) is larget than 0.125, so we have .001 for starters, we need 0.075 more, which is between 1/16 and 1/8, so we have 1/8 + 1/16 so far (0.125 + 0.0625 = 0.1875, .0011 in decimal). We would need 0.0125 next, which, again, is not a sum for 2^-x powers and 0.2 representation is again truncated. You get close, but never 0.2 exactly.

This is why you're not getting 0.3 for adding 0.1 and 0.2, which also doesn't have an exact binary representation. 0.1 + 0.2 in binary is like adding 2 truncated numbers, the way you would do 975 + 210 in decimal, about 1000 + about 200, that should be about 1200.

SilviuBurcea
Автор

I mean... labels are essentially GOTO statements. Sure, there are times when you want to use them, but they are usually a bad thing because it makes code less readable in more complex examples. For this first example, you can just use a "break" statement to break out of that inner loop.

lukahadziegric
Автор

I'm amazed that after so long of working in JS that I can still learn something new. This time around, the console Timer and debugger option :) Thanks for sharing!

Bazkur
Автор

I mean, for the "Binary Math" solution I would highly recommend using Math.round and just round the comparing values to 2 decimals or so. Or directly round the values after each math-operation to x decimals so you can then later compare them to 0.35 directly.

mhombach
Автор

This video is like window shopping, you find things you never thought you needed!

GetawayJamie
Автор

On Binary Math. I would advise multiplying, rounding and diving to significant decimals to ensure that the input condition is treated to match our condition.
e.g. Math.round(num * 100) / 100 for 2 decimal.

Same with strings :
Always do a if( {input}.toLowerCase().trim() == ) {}

I would say these 2 methods are life savers when you want String and Number comparison.

CaddyBlue
Автор

Tip for those trying to compare equality on decimal arithmetic: Use subtraction and an epsilon. IE: let epsilon = 0.00001 (or whatever precision you like) and re-write your equality to if Math.abs(x-Comparison_Value) < epsilon { }. In your example it would be if (Math.abs(x-0.35) < epsilon) { // do stuff }

KurtSchwind
Автор

IMO if you need labels, that is a code-smell

KyleLanmon
Автор

9:58 No, it's not hard at all. You can use Object.keys, Object.values and Object.entries to effortlessly loop through anything you want. Being able to use an object as a key is super cool though! Would be even cooler if object comparison wasn't such a pain in JS 😅

magicfibre
Автор

17:12 you can use Number.EPSILON in combination with Math.abs() to include these tolerances. Eg.: Math.abs(x - 0.35) <= Number.EPSILON

revidee
Автор

Thanks for all the great info! I hope your new courses make you enough money to finally afford furniture

dave
Автор

Another cool but rare piece of JS goodness: JS Proxy API.

anticsBack
Автор

The For labels seem like a great way of documenting what each of your loops is doing without having to use comments. Even if you don't use them in the execution itself. Just like good variable names avoid unnecessary comments.

AeroPR
Автор

these console methods are pretty neat, they sure'll make debugging a little less of a pain

maelstrom
Автор

It's always a bad idea to assert equally with floating point values. They're mostly useful in games, math, geo spacial and other applications where accuracy to 1/10000ths are not important. Always use whole integers when dealing with money values where 1 is the smallest unit of a given currency, and use number formatting when displaying to a UI.

gosnooky
Автор

This is really well done! I like the way you structure your video. The code, output, and video layout makes it easy to follow. Keep it up!

jameswalker
Автор

I often make a helper function to create "enums" in JS (obvs. not real enums, but close enough for most uses). It just takes a list of strings, and then returns a *frozen* object that maps the strings to Symbols of the string, thereby ensuring that (a) they can't be changed, and (b) they are all unique values. Which is like 2/3 of the use of enums.

So for instance, I could do:

const Directions = make_enum('UP', 'DOWN', 'LEFT', 'RIGHT');

And the resulting frozen object would have unique symbols referenced as Directions.UP, Directions.DOWN, Directions.LEFT, and Directions.RIGHT.

IceMetalPunk
Автор

Note: You need unique ids when using console.time. Generate a random suffix for the name when using it in async functions that are called multiple times.

z-a
Автор

When I work with large decimals I use .toFixed() with a + before to round and convert back to a number. Like this: `+num.toFixed(12)`

mattd