menu_book Course Curriculum (JavaScript) expand_more
Array reduce
Learn Array reduce in JavaScript through one focused, topic-specific example.
Array reduce is a JavaScript language or platform feature inside Arrays and Collections. The example executes the exact construct named here and keeps unrelated logic out so the observable result comes from this feature itself.
Trace the values before and after the operation, then consider the edge case below. In JavaScript, coercion, scope, object identity, scheduling, and host-environment rules are frequent sources of surprising behavior.
Topic-specific example
const lines=[{qty:2,price:5},{qty:3,price:7}];
const total=lines.reduce((sum,l)=>sum+l.qty*l.price,0);
console.log(total);
Several array methods mutate the original array while others return a new one; verify which contract the method uses before reusing references.