Asked 7 years ago
30 Nov 2016
Views 841
duglus

duglus posted

use of every function in array manipulation in JavaScript

i used many time many function for array manipulation in JavaScript but never used every function so what is use of every function in array manipulation in JavaScript ?

var javascript = ["i",'am',"javascript"]
javascript.every();//what it does ?


explain please . if it helpful . i will use it in my script next time .
shyam

shyam
answered Nov 30 '-1 00:00

you cant call like this

javascript.every();

because it need callback function to pass as argument in every function in JavaScript
every function iterate every value untill it get true from call back function .

var javascript = ["i",'am',"javascript"]
javascript.every(function iterate(value){
   alert(value);
   return true;
});

it give you three alread for "i",'am',"javascript"


var javascript = ["i",'am',"javascript"]
javascript.every(function iterate(value){
   alert(value);
});

it give you one alert because it dont return anything means return false in first iteration so after first iteration it does not call second time.

Usage::
mostly iterate through array with callback function

Example
suppose if you find all array value should be not empty in JavaScript or find even or odd value from array etc..

shyam

shyam
answered Nov 30 '-1 00:00

every function in javascript work as it as its name stand for .

it will loop through every value of the array . in this loop , logic will placed on the call back function.
Post Answer