every Array Method | JavaScript Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
the array every method executes a given function on every items of an array and returns true only if the callback function returns a true T value for all the items inside the array if a false value is found it will return false immediately let's see how this works in code you can see here we have an array of numbers from 1 to 5 let's say we want to check if all of the numbers are positive so we're going to call the every method loop on the numbers array this method requires a callback function and that callback function will be provided with value index and array which can be used I'm going to write here the callback function let's call it is positive and it can get the item the index and the array but in this case we won't need index an array so I'm going to remove them and here we can return if the item is greater than 0 so if it is a positive number and this is positive callback function can be put there and now if we console that flog the result I forgot to store it here we can see that in the right we now get through because all of the elements are bigger than zero what if we add minus one here at the beginning now we're going to get false because one of the items inside the array is not positive let's see another example of when we can use this function we can test if we have an array of objects if all the objects inside of that array have a certain property so let me quickly paste in the array of objects all right so here we have an array of objects each object has a name property except the last one so let's test if all of the objects inside the person's array have the name property so conce the result will be persons that every and here we can provide an error function and for every person we're going to test if the person that name is different of undefined so we can check if we have that property undefined all right then let's console the clock press and here we go we get false because the last one here J its surname let's change this to name and now we should get through which is true or another example might be let's say we want to have an array of arrays 1 2 3 4 5 6 7 8 9 and we want to check if all of the items inside the let's change this actually arrays like that and we want to check if all of the Rays are actually arrays array is array this array were providing oh I messed up the array here are : and here also and we need to remove the parentheses all right like that okay so now we have an array of arrays and here we get through but if for example this will become a string 1 2 3 or something like that we'll get false because this is not an array but I guess by now you get how every method works I hope you enjoyed this tutorial and I'll see you in the next one bye [Music] you
Info
Channel: Florin Pop
Views: 4,278
Rating: 4.9349594 out of 5
Keywords: javascript, javascript tutorial, javascript tutorial for beginners, array method, javascript arrays, higher order function, learn javascript, javascript array methods, every, javascript every, every array method, learn every, every HOF
Id: d9WGXz-DLT8
Channel Id: undefined
Length: 4min 17sec (257 seconds)
Published: Fri Apr 03 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.