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
@chetanjain46163 жыл бұрын
Never thought I would learn so much from such a basic concept that I have basically used a million times. Thanks Dave
@DaveGrayTeachesCode3 жыл бұрын
You're welcome! I'm glad I could help.
@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.
@DaveGrayTeachesCode2 жыл бұрын
Thank you, Raven! 💯
@caffeinated-pixels3 жыл бұрын
Super useful video Dave. I wasn't even aware of optional chaining! I'll be bookmarking this vid for future use 😀
@DaveGrayTeachesCode3 жыл бұрын
Glad to hear it! Thanks for the note 💯
@laztheripper2 жыл бұрын
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 !
@DaveGrayTeachesCode2 жыл бұрын
Agreed! I cannot remember how much I may have explained about those but you are spot on 💯
@RD-jr8nv Жыл бұрын
Your style of teaching is great man. Loving your videos.
@DaveGrayTeachesCode Жыл бұрын
Glad you like them!
@ahmad-murery3 жыл бұрын
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
@DaveGrayTeachesCode3 жыл бұрын
Spot on as usual, Ahmad! Thanks once again my friend. Your comments are always welcome! 💯🙏
@ahmad-murery3 жыл бұрын
@@DaveGrayTeachesCode Thank you for the great work my friend
@abellara91693 жыл бұрын
Perfect timing for this video. I'll apply this to my current project. Cheers Dave. 👍
@DaveGrayTeachesCode3 жыл бұрын
Glad I could help! 💯
@jesustzinon2 жыл бұрын
Just found your channel, i really like the way you explain these things, greetins from venezuela
@DaveGrayTeachesCode2 жыл бұрын
Thank you!
@eduardoADSL3 жыл бұрын
I did a lot of this in Dart language and it's good to know you also can do this in javascript.
@DaveGrayTeachesCode3 жыл бұрын
Glad to help!
@albiummid2 жыл бұрын
Very effective video at all.
@jorgebarragan85212 жыл бұрын
Very useful information!!
@DaveGrayTeachesCode2 жыл бұрын
Glad it was helpful! 💯
@GabrielMartinez-ez9ue2 жыл бұрын
I love these videos... please keep making them
@DaveGrayTeachesCode2 жыл бұрын
Thank you! 🙏 I'll keep going! 🚀
@malemyengkhom563 жыл бұрын
Your explanation is best ❤️🔥 Please make a video on prototype 🙏🙏
@DaveGrayTeachesCode3 жыл бұрын
Thank you. I have a prototypes tutorial here: kzbin.info/www/bejne/o4KXoHadmMx7haM
@aileenchan37412 жыл бұрын
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.
@DaveGrayTeachesCode2 жыл бұрын
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.
@aileenchan37412 жыл бұрын
@@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_f2 жыл бұрын
excellent video! thanks
@DaveGrayTeachesCode2 жыл бұрын
You are welcome!
@piyushmahapatra54023 жыл бұрын
Typescript solves this elegantly.
@DaveGrayTeachesCode3 жыл бұрын
I need to do some Typescript here, too. Thanks for the note! 🚀
@코린이31세3 жыл бұрын
Always amazing video!! thank you sir
@DaveGrayTeachesCode3 жыл бұрын
You're very welcome! 🙏💯
@sergiorey20493 жыл бұрын
This is really helpful, thanks
@DaveGrayTeachesCode3 жыл бұрын
You're welcome 🙏
@AM-rf4cp3 жыл бұрын
Cool man. as always!!!
@DaveGrayTeachesCode3 жыл бұрын
Thank you A M! 💯
@abdullahsevmez423 жыл бұрын
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
@DaveGrayTeachesCode3 жыл бұрын
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! 💯🙏
@abdullahsevmez423 жыл бұрын
@@DaveGrayTeachesCode Okay I will contact with you thank you for what you are doing! :) glad to meet you
@rodolfoamandiodias76702 жыл бұрын
Nice my friend ✋🇧🇷
@meeeh2 жыл бұрын
What do you think about console.log(!!(myArray?.length)) Is it okay?
@DaveGrayTeachesCode2 жыл бұрын
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.
@rajikkali23813 жыл бұрын
Great idea, doesn’t work in Vue inline template statements though.
@DaveGrayTeachesCode3 жыл бұрын
Good to know. This is just Vanilla JS which makes me curious. Is Vue blocking the intended functionality?
@rajikkali23813 жыл бұрын
@@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
@DaveGrayTeachesCode3 жыл бұрын
@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.
@DaveGrayTeachesCode3 жыл бұрын
@@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.
@ijajulislam72233 жыл бұрын
sir make a advance js project for using whole concept that we have learned from your js playlist
@DaveGrayTeachesCode3 жыл бұрын
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! 💯🚀
@abelmarkos57533 жыл бұрын
Can you make a video explaning how can we distruct an array with nasted object inside it
@DaveGrayTeachesCode3 жыл бұрын
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.
@abelmarkos57533 жыл бұрын
@@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
@DaveGrayTeachesCode3 жыл бұрын
@@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