I was checking for empty Arrays wrong... | How to check for an empty array in Javascript

  Рет қаралды 13,684

Dave Gray

Dave Gray

Күн бұрын

Пікірлер: 62
@DaveGrayTeachesCode
@DaveGrayTeachesCode 3 жыл бұрын
If you only check the length property of an array, you may have a problem if the expected array is undefined or null instead. To avoid this, many of us use the expression: array && array.length ...but now there is a new and more concise way to do this. In this tutorial, I explore this new way to check for empty arrays and also discuss how to insure you have an array and how to check for object properties within an array. New to Javascript? Consider starting with my full 8 hour Javascript tutorial video here: kzbin.info/www/bejne/e5eknWyYrN-JkM0
@chetanjain4616
@chetanjain4616 3 жыл бұрын
Never thought I would learn so much from such a basic concept that I have basically used a million times. Thanks Dave
@DaveGrayTeachesCode
@DaveGrayTeachesCode 3 жыл бұрын
You're welcome! I'm glad I could help.
@ravenMK_
@ravenMK_ 2 жыл бұрын
I've learnt something new "null coalescing". I knew about it, just never knew when to use it. You changed my way of thinking about API. I've been a victim of always trusting the data an API returns. My application crashed when it received something else. Now I'm so gutted I have to refactor it to make it more robust. Thank you Dave. Love your videos.
@DaveGrayTeachesCode
@DaveGrayTeachesCode 2 жыл бұрын
Thank you, Raven! 💯
@caffeinated-pixels
@caffeinated-pixels 3 жыл бұрын
Super useful video Dave. I wasn't even aware of optional chaining! I'll be bookmarking this vid for future use 😀
@DaveGrayTeachesCode
@DaveGrayTeachesCode 3 жыл бұрын
Glad to hear it! Thanks for the note 💯
@laztheripper
@laztheripper 2 жыл бұрын
Worth mentioning the difference between || and ??. ?? Handles only null and undefined while || will work for 0 and "" as well. Both have their uses. Good stuff !
@DaveGrayTeachesCode
@DaveGrayTeachesCode 2 жыл бұрын
Agreed! I cannot remember how much I may have explained about those but you are spot on 💯
@RD-jr8nv
@RD-jr8nv Жыл бұрын
Your style of teaching is great man. Loving your videos.
@DaveGrayTeachesCode
@DaveGrayTeachesCode Жыл бұрын
Glad you like them!
@ahmad-murery
@ahmad-murery 3 жыл бұрын
Hello Dave, at 4:28 I think we got true here because the id property does exist and it evaluates to true, the same is true for the name property which returns undefined so the ternary operator choses the false part there is one older way to check for array which is using the instanceof operator myArray instanceOf Array I really appreciate this type of videos, it makes you more aware of some problems that are hard to predict. Thanks my friend
@DaveGrayTeachesCode
@DaveGrayTeachesCode 3 жыл бұрын
Spot on as usual, Ahmad! Thanks once again my friend. Your comments are always welcome! 💯🙏
@ahmad-murery
@ahmad-murery 3 жыл бұрын
@@DaveGrayTeachesCode Thank you for the great work my friend
@abellara9169
@abellara9169 3 жыл бұрын
Perfect timing for this video. I'll apply this to my current project. Cheers Dave. 👍
@DaveGrayTeachesCode
@DaveGrayTeachesCode 3 жыл бұрын
Glad I could help! 💯
@jesustzinon
@jesustzinon 2 жыл бұрын
Just found your channel, i really like the way you explain these things, greetins from venezuela
@DaveGrayTeachesCode
@DaveGrayTeachesCode 2 жыл бұрын
Thank you!
@eduardoADSL
@eduardoADSL 3 жыл бұрын
I did a lot of this in Dart language and it's good to know you also can do this in javascript.
@DaveGrayTeachesCode
@DaveGrayTeachesCode 3 жыл бұрын
Glad to help!
@albiummid
@albiummid 2 жыл бұрын
Very effective video at all.
@jorgebarragan8521
@jorgebarragan8521 2 жыл бұрын
Very useful information!!
@DaveGrayTeachesCode
@DaveGrayTeachesCode 2 жыл бұрын
Glad it was helpful! 💯
@GabrielMartinez-ez9ue
@GabrielMartinez-ez9ue 2 жыл бұрын
I love these videos... please keep making them
@DaveGrayTeachesCode
@DaveGrayTeachesCode 2 жыл бұрын
Thank you! 🙏 I'll keep going! 🚀
@malemyengkhom56
@malemyengkhom56 3 жыл бұрын
Your explanation is best ❤️🔥 Please make a video on prototype 🙏🙏
@DaveGrayTeachesCode
@DaveGrayTeachesCode 3 жыл бұрын
Thank you. I have a prototypes tutorial here: kzbin.info/www/bejne/o4KXoHadmMx7haM
@aileenchan3741
@aileenchan3741 2 жыл бұрын
Another great video!! Thank you Dave!! I learned a lot. Do you have a video explaining permutations or combinations? I’m still unable to grasp the concept. I will check your channel.
@DaveGrayTeachesCode
@DaveGrayTeachesCode 2 жыл бұрын
Thanks! The term permutations usually references changing data as in array permutations (changes). Are you referring to combinations in the same way? Needing a reference point here, but you may want to watch my video on deep copy vs shallow copy. It addresses comparing data and why we avoid permutations when updating state.
@aileenchan3741
@aileenchan3741 2 жыл бұрын
@@DaveGrayTeachesCode Yes, I saw those videos and they helped my understanding of deep copy. Thank you so much, and sorry If I didn't make sense before. What I meant by Permutations is when given an array say, ['a', 'b', 'c'], create a function that generates possible versions of the array. So the output would be an array of arrays, like: [['a', 'b', 'c'], ['a', 'c', 'b'], ['b', 'a', 'c'], ['b', 'c', 'a'], ['c', 'a', 'b'], ['c', 'b', 'a']] ...something like that. I'm having trouble understanding how the switch works.
@l_t_m_f
@l_t_m_f 2 жыл бұрын
excellent video! thanks
@DaveGrayTeachesCode
@DaveGrayTeachesCode 2 жыл бұрын
You are welcome!
@piyushmahapatra5402
@piyushmahapatra5402 3 жыл бұрын
Typescript solves this elegantly.
@DaveGrayTeachesCode
@DaveGrayTeachesCode 3 жыл бұрын
I need to do some Typescript here, too. Thanks for the note! 🚀
@코린이31세
@코린이31세 3 жыл бұрын
Always amazing video!! thank you sir
@DaveGrayTeachesCode
@DaveGrayTeachesCode 3 жыл бұрын
You're very welcome! 🙏💯
@sergiorey2049
@sergiorey2049 3 жыл бұрын
This is really helpful, thanks
@DaveGrayTeachesCode
@DaveGrayTeachesCode 3 жыл бұрын
You're welcome 🙏
@AM-rf4cp
@AM-rf4cp 3 жыл бұрын
Cool man. as always!!!
@DaveGrayTeachesCode
@DaveGrayTeachesCode 3 жыл бұрын
Thank you A M! 💯
@abdullahsevmez42
@abdullahsevmez42 3 жыл бұрын
I'm following you for 1 week and your contents are perfect! I would know you in real world! I'm waiting from you for nodejs socket io module in real project with event emitter module and authentication with google, facebook! You are doing well bro I really want to know you, that after you have someone in turkey :) if you come
@DaveGrayTeachesCode
@DaveGrayTeachesCode 3 жыл бұрын
Thank you, Abdullah! You can connect with me at several social media links in the description. I do want to travel more, and I appreciate the invitation. Thank you for your comment! 💯🙏
@abdullahsevmez42
@abdullahsevmez42 3 жыл бұрын
@@DaveGrayTeachesCode Okay I will contact with you thank you for what you are doing! :) glad to meet you
@rodolfoamandiodias7670
@rodolfoamandiodias7670 2 жыл бұрын
Nice my friend ✋🇧🇷
@meeeh
@meeeh 2 жыл бұрын
What do you think about console.log(!!(myArray?.length)) Is it okay?
@DaveGrayTeachesCode
@DaveGrayTeachesCode 2 жыл бұрын
You are using the "double bang" operator !! to get a boolean true or false value for output. If that is what is needed, it is ok. If not, 0 is considered false already.
@rajikkali2381
@rajikkali2381 3 жыл бұрын
Great idea, doesn’t work in Vue inline template statements though.
@DaveGrayTeachesCode
@DaveGrayTeachesCode 3 жыл бұрын
Good to know. This is just Vanilla JS which makes me curious. Is Vue blocking the intended functionality?
@rajikkali2381
@rajikkali2381 3 жыл бұрын
@@DaveGrayTeachesCode I don’t think so, but perhaps Vue’s parser just can’t handle it for some reason. I can use it directly in methods, but inline template scripts (which are slightly different) can use normal ternary, but neither of ‘?.’ or ‘??’ syntaxes work. Which sucks, because I always try to use it and it backfires on me. It would make inline scripts shorter, for sure
@DaveGrayTeachesCode
@DaveGrayTeachesCode 3 жыл бұрын
​ @Rajik Kali ah, I understand. Both optional chaining and null coalescing are newer JS features. Maybe they will get Vue inline template support in the future.
@DaveGrayTeachesCode
@DaveGrayTeachesCode 3 жыл бұрын
@@angelajholden thanks! That's not wrong. One of the last examples I gave included checking the first position as you are. If optional chaining did work there, I'd do something with it - but it sounds like it doesn't for Vue inline template statements right now according to Rajik.
@ijajulislam7223
@ijajulislam7223 3 жыл бұрын
sir make a advance js project for using whole concept that we have learned from your js playlist
@DaveGrayTeachesCode
@DaveGrayTeachesCode 3 жыл бұрын
Thank you for asking. I do have some Vanilla JS projects here: kzbin.info/aero/PL0Zuz27SZ-6PA3nkkEAr91n4bK-NunwPU You may be asking for more advanced projects than that playlist contains? I will be creating more. Thanks! 💯🚀
@abelmarkos5753
@abelmarkos5753 3 жыл бұрын
Can you make a video explaning how can we distruct an array with nasted object inside it
@DaveGrayTeachesCode
@DaveGrayTeachesCode 3 жыл бұрын
I can, but I can also quickly reply that I would map over the array if it contained multiple objects. You could use the higher order function map() to pull out a specific property value into an array of values.
@abelmarkos5753
@abelmarkos5753 3 жыл бұрын
@@DaveGrayTeachesCode thank you for replying to my comment I know about map What i meant was like in react project if we want to change the state of a component which consist of array with nasted object we have to distruct the array and every object inside it to the lowest level before update the state
@DaveGrayTeachesCode
@DaveGrayTeachesCode 3 жыл бұрын
@@abelmarkos5753 considering React, you would make a copy of the immutable state object. It won't always be through destructuring though. I do have a tutorial on making shallow copies and deep copies of objects. I think you will find the deep copy function helpful for what you want: kzbin.info/www/bejne/anbNYX-tn6h3kLM
@Albert_Hall
@Albert_Hall Жыл бұрын
Great great great
@DaveGrayTeachesCode
@DaveGrayTeachesCode Жыл бұрын
🙏🙏
@hosamgnaba3205
@hosamgnaba3205 2 жыл бұрын
great
@js_warlock
@js_warlock 2 жыл бұрын
One more: arrayVar instanceof Array
@StuartAylward
@StuartAylward 8 ай бұрын
typeof myArray === "array" 😅
Shallow Copy and Deep Copy | Advanced Javascript Tutorial
26:36
1, 2, 3, 4, 5, 6, 7, 8, 9 🙈⚽️
00:46
Celine Dept
Рет қаралды 56 МЛН
Try Not To Laugh 😅 the Best of BoxtoxTv 👌
00:18
boxtoxtv
Рет қаралды 7 МЛН
5 Tips for Writing BETTER For Loops in JavaScript
16:15
James Q Quick
Рет қаралды 70 М.
Why Is Array/Object Destructuring So Useful And How To Use It
13:24
Web Dev Simplified
Рет қаралды 429 М.
5 Real Life Examples of Array Reduce in JavaScript
12:47
What is __proto__ ? | Javascript Prototypes Tutorial
23:11
Dave Gray
Рет қаралды 44 М.
What are Pure Functions? | Javascript Functions Tutorial
15:43
Tips For Using Async/Await in JavaScript
16:26
James Q Quick
Рет қаралды 396 М.
Get a Unique List of Objects in an Array of Object in JavaScript
20:35
Yagisanatode -Scott-
Рет қаралды 32 М.