Javascript Array every() Method to Check Condition on All Array Elements Full Example
Jan 9, 2025
Buy the full source code of application here:
Show More Show Less View Video Transcript
0:00
uh Hello friends welcome to this video
0:02
so in this video we will look at another
0:04
JavaScript array method I this method is
0:08
used for conditional purpose so if you
0:10
have a array of values and then you can
0:14
actually test out a function against
0:17
these values so this method is actually
0:19
called as
0:20
every so let's suppose you
0:23
declare a array a set of Ages you have
0:28
inside so all these values will be
0:31
numeric so 45 40
0:35
35 30 25 so we actually declared an
0:40
array here ages so it basically holds
0:43
ages for different persones so this is
0:45
person one person two person three
0:47
person four person five so five ages we
0:50
have put right here in this array and
0:53
now we need to write a
0:55
function to actually
0:57
test that all all these ages it should
1:02
be above it should be less than 50 so
1:07
now to write that function we will write
1:09
a function custom function this function
1:11
name can be anything so we have written
1:14
check is so what this function does it
1:17
actually takes the age as an argument so
1:21
one by one it will test out all the
1:23
values of this array one by one can see
1:27
that so now it will it can be anything
1:31
I'm just taking example right here which
1:32
is ages so you're putting it and what
1:36
this function does the business logic of
1:39
this function is it will all just return
1:42
the AG if it
1:45
is greater if it is smaller than
1:48
50 this is actually a condition that we
1:51
put right here for each age that you
1:54
pass you will compare it that this a
1:57
should be smaller than 50
2:01
so one by one now we will actually
2:04
invoke this so now to actually call this
2:07
function we actually write the function
2:10
array name which is ages and this
2:13
actually is the every method that we are
2:15
talking about and right here we will
2:19
pass the function
2:20
name right as an argument in
2:24
this so we'll simply pass this function
2:27
which we want to check so what this
2:29
function does uh it actually returns
2:33
either a true or false value you can see
2:36
nothing is print printed out in the
2:38
console we need to print it out so the
2:41
value we need to
2:42
give so we can directly console log this
2:49
value so if you now see uh True Value is
2:52
returned to
2:54
us why it is happening because if you
2:57
check it is actually executing this
3:01
function for every array value that we
3:03
have check age function so this ages
3:06
function 45 if you directly test out 45
3:11
is smaller than 50 so it is true and
3:14
then 40 is also smaller than 50 true 35
3:18
also is true 30 uh smaller than 50 true
3:23
25 smaller than 50 true so it actually
3:26
test out all the values which is present
3:28
in the array one by one and uh after
3:31
that it will exactly tell you if all the
3:34
values comes out to be true then the
3:36
value returned right here is true let's
3:39
suppose if I make any sort of adjustment
3:42
right here if I change this from 40 to
3:44
60 so now what happens condition
3:46
evolutes to false because 60 is not
3:49
smaller than 50 so this condition
3:52
evalues to false so in that case this
3:56
becomes from True to false you'll see
3:59
that so if any value if any value in the
4:03
array just breaks the condition in that
4:07
scenario it this function will return
4:09
false in either of the cases in either
4:11
of the elements it breaks the condition
4:14
then it will return false for for it for
4:18
this function to return true it needs to
4:20
pass all the values so all the values
4:24
must ader to the condition that you put
4:26
right here so this can be anything this
4:29
function can be anything anything so
4:30
here in this case we are checking the
4:32
ages the age must be below 50 so in our
4:37
case this value is breaking this
4:39
condition so that's why false is printed
4:42
out so if I once again change it to 30
4:45
so once again it will change from false
4:47
to true so in this way you can actually
4:50
use this function for condition and
4:53
purposing in application so if you have
4:55
a set of values that you want to check
4:57
for a certain condition you can check
4:59
this you can pass the function name
5:02
inside this every function so if you
5:04
want to test out to multiple
5:07
conditions you can Define another
5:09
function right here so if you I can
5:13
say
5:15
check
5:17
ag2 so age must be smaller than 70 as
5:20
well if you want to pass
5:23
multiple functions so we can I think we
5:26
can pass it put a comma
5:30
so again it returns to true because uh
5:34
age is smaller than 50 so again age is
5:38
smaller than 70 as well because all
5:40
these values are smaller than 70 so if I
5:43
make some modification change from 35 to
5:46
80 so what this happen it again changed
5:49
to false because 80 is
5:53
uh not smaller than 50 or
5:58
70 so in this way you can pass multiple
6:01
conditions multiple functions to this
6:03
function every function this is a very
6:06
useful function in arrays by which you
6:08
can test out different functions on all
6:12
the elements of the
6:14
array so it doesn't modify the array so
6:17
after you print out this value it
6:19
doesn't modify the array it just test
6:21
out conditions based upon the functions
6:23
that you write so in this way uh thank
6:27
you very much for watching this video if
6:30
you do like the video please hit that
6:31
like button subscribe the channel and do
6:33
check out my website free mediat
6:35
tools.com which contains open source
6:37
tools regarding audio video and image
6:40
and I will be seeing you in the next
6:42
video
