JavaScript Arrays: Properties, Methods, and Manipulation (Part 6 of 7)

Corey Schafer · Beginner ·🌐 Frontend Engineering ·11y ago

Key Takeaways

This video series covers JavaScript Arrays, focusing on properties, methods, and manipulation, specifically the reduce() and reduceRight() methods for array manipulation and data processing.

Full Transcript

so reduce and reduce right are uh one of the harder methods to grasp just because um instead of running an array instead of running every element through a function and then getting a response reduce and reduce right will actually remember the previous response and apply it the next time around and i don't think i can describe it better than the chart over on the mozilla developer network so i'm just going to show you one of their examples here if you look at their example they have an array that is just 0 1 2 3 4 and then they call reduce and then within reduce they are using their callback function and so far in this tutorial we've been pulling these call callback functions out and naming them but putting them inside here is basically the same thing so instead before we have usually called an element here and we've done element and then index and then array but with reduce we have previous value and current value and you can see here that they return previous value plus current value so there is two ways to call reduce you can either not provide an initial value or you can provide an initial value and if you don't provide an initial value then the first time through the first time it runs this function the previous value will be set to the first value of the array the current value will be the second element in the array if you do provide an initial value down here they have one with an initial value of 10 and if they do provide one instead of this instead of the first time through the function instead of the previous value equaling the first element of the array it equals the initial value that you passed in and the current value equals that first element of the array but pretty much if we go through this step by step what's happening here is it calls this function on the on the first positions of the array so like i said here there's no initial value so the previous value zero the current value is one and what we're returning is previous value plus current value so zero plus one and then we can see over here they have the return value listed zero plus one is one and now this return value is saved as the previous value the next time around so now uh if we come down here to the second call this previous value is now equal to one and then the current value is the next value in the array which is two so one plus two is three so that three gets saved as the previous value for the next call so three and then we move on to the next value in the array which is now three so three plus three is six and then six it's carried over to the previous value again and then the next value in the array is four so previous value is six current value is 4 6 plus 4 equals 10. so basically this reduced function is just a way to sum up all these values and if you provide the initial value here and it does the same thing except it just has this one extra call at the beginning that takes the initial value and adds it to the first element of the array and then that return value gets passed in as the previous value for the second call and it moves to the next index so if i pull this up in our examples here we have this array that is one two three four five if i made a function called add values and remember we'll have previous we'll have current and we can also pass in the index and we can also pass in the array so what we'll do here we'll just i'll just show you the example that they had so let's return previous plus current and then let me uncomment out this result variable here and we'll set the result equal to ray17 dot reduce and we will pass in add values and save that and let me output this to the screen and you can see that the reduced result is 15 because 1 plus 2 is 3 3 plus 3 is 6. 6 plus 4 is 10 and 10 plus 5 is 15. so what if we did this with an array of characters so i'll uncomment that and now you can see this reduced result is a b c d e because the first time through it does a plus b and then a plus b which is a b it's passed in as the previous value so then the next time through you have a b plus c and then the next time through you have abc plus d and then you have a b c d plus e so that's how that works and i grouped together reduce and reduce right to show you at the same time because reduce right instead of starting at the zeroth element and working to the right what reduce right does is it starts at the end and it just goes to the left so if i was to do reduce right to these add values then you can see now this reduce result is reversed because it starts off here at e and e is the previous value current value is d so it says e plus d which is e d then the next time through ed is the previous value and then ed plus c is edc then edc plus b and then edc b plus a and then you get uh the entire reversed string as the result so hopefully that makes sense i know that this is one of the more complicated methods that we took a look at um just to come kind of hammer it in let's take a look at doing this with an object so let me uncomment out this and okay now you have to be careful when you're working with objects and this is why reduce can be a little complicated because you might run into some things that you didn't expect so for example if i was to do total age here then okay so we give previous current and then we'll just leave off the index and array since we're not using it so you would think that we could just do something like this previous.age plus current.h and you think that this would go through all of these elements here and see and add up all the ages the sum total total of the ages just like it did for the integers but if we run this bar result let's do array this is array 17 ray17 dot reduce and this is total age and save that and let's output this to the screen okay you can see that the reduced result is not a number so what's the deal here what happened well if we look at this further then you see i have some comments here whenever we first run through this total age function it starts off since we don't have an initial value the previous value will be the first down here where we've printed out the original this previous value will be an object with name quarry and age 28 and the current will be an object with the name john and age 52 so we're saying previous dot age plus current that age so 28 plus 52 that returns 80. okay so that's what we expected but then the next time through the next time it calls total age this return value gets passed in as previous so then we get to the point where it gets to previous dot age plus current dot age but it sees this 80 and this 80 doesn't have an age property so it doesn't know what to do and then that's where it kind of blows up so how can we solve something like this it's just something that we need to be aware of so we can do probably the best way there's probably multiple ways to solve this but probably one of the better ways is just to pass in an initial value of 0 and then here instead of previous.age we could just do previous plus current.h and now what happens here whenever you pass in an initial value let me go ahead and change what my example is down here so let me move some of this stuff around okay so previous will be the initial value of zero current is going to be the first value of our array which is an object with the name query and the age 28 so then we run previous which is zero plus current.age which is 28. so 0 plus 28 is 28 and then this return value gets assigned to previous on the next call so we have previous which is 28 um plus we're not looking up the dot age property so it's previous which is 28 plus current.age which is this object here 52 so this will return 80. save that and then the next time through it'll do 80 plus uh 36 which you can see down here now on in our html it's outputting uh 116. so that's what we expect when it adds up all those ages so that's just something that you have to look out for it might not be you have to keep in mind not only what's going to happen the first time you run through the method or through the method reduce but you have to keep in mind what it's going to be looking at every time it runs through and what previous values you're going to be or what values you're going to be assigning to that previous variable so hopefully that was a good look at reduce and you understand how that works so now let's take a look at the map method

Original Description

JavaScript Arrays: Properties, Methods, and Manipulation (Part 6 of 7) In this series, we will take an in-depth look at JavaScript Arrays and everything we can do with them. In part 6, we will go over: reduce() and reduceRight() Part 1: https://youtu.be/8JgU2WmrZXI Part 2: https://youtu.be/nAWVYFEzoY8 Part 3: https://youtu.be/cdPS-lmlwco Part 4: https://youtu.be/JskeRdu_X8Q Part 5: https://youtu.be/w4KF_lapbRI ... Part 7: https://youtu.be/qxzp4X6sfGo ✅ Support My Channel Through Patreon: https://www.patreon.com/coreyms ✅ Become a Channel Member: https://www.youtube.com/channel/UCCezIgC97PvUuR4_gbFUs5g/join ✅ One-Time Contribution Through PayPal: https://goo.gl/649HFY ✅ Cryptocurrency Donations: Bitcoin Wallet - 3MPH8oY2EAgbLVy7RBMinwcBntggi7qeG3 Ethereum Wallet - 0x151649418616068fB46C3598083817101d3bCD33 Litecoin Wallet - MPvEBY5fxGkmPQgocfJbxP6EmTo5UUXMot ✅ Corey's Public Amazon Wishlist http://a.co/inIyro1 ✅ Equipment I Use and Books I Recommend: https://www.amazon.com/shop/coreyschafer ▶️ You Can Find Me On: My Website - http://coreyms.com/ My Second Channel - https://www.youtube.com/c/coreymschafer Facebook - https://www.facebook.com/CoreyMSchafer Twitter - https://twitter.com/CoreyMSchafer Instagram - https://www.instagram.com/coreymschafer/
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Corey Schafer · Corey Schafer · 22 of 60

1 Web fonts using CSS Font Face
Web fonts using CSS Font Face
Corey Schafer
2 Using Font Awesome in Desktop Applications (OS X)
Using Font Awesome in Desktop Applications (OS X)
Corey Schafer
3 Sublime Text 2: Setup, Package Control, and Settings
Sublime Text 2: Setup, Package Control, and Settings
Corey Schafer
4 ArcGIS API for JavaScript Part 1: Our First Web Map
ArcGIS API for JavaScript Part 1: Our First Web Map
Corey Schafer
5 Mac Tip: Windows' Snapping Feature on Mac with HyperDock
Mac Tip: Windows' Snapping Feature on Mac with HyperDock
Corey Schafer
6 Linux/Mac Terminal Tutorial: Creating Aliases for Commands
Linux/Mac Terminal Tutorial: Creating Aliases for Commands
Corey Schafer
7 ArcGIS API for JavaScript Part 2: Starting Templates
ArcGIS API for JavaScript Part 2: Starting Templates
Corey Schafer
8 Paver Patio Time Lapse
Paver Patio Time Lapse
Corey Schafer
9 Mac Tip: Ways to perform Screen Capturing and Screenshots
Mac Tip: Ways to perform Screen Capturing and Screenshots
Corey Schafer
10 WordPress Plugins: Imsanity
WordPress Plugins: Imsanity
Corey Schafer
11 WordPress Tips: Test your theme with Theme Unit Test and Monster Widget
WordPress Tips: Test your theme with Theme Unit Test and Monster Widget
Corey Schafer
12 Sublime Text 3: Setup, Package Control, and Settings
Sublime Text 3: Setup, Package Control, and Settings
Corey Schafer
13 Understanding Binary, Hexadecimal, Decimal (Base-10), and more
Understanding Binary, Hexadecimal, Decimal (Base-10), and more
Corey Schafer
14 Mac Tip: Adding Folder Stacks to the Dock
Mac Tip: Adding Folder Stacks to the Dock
Corey Schafer
15 CSS Tips and Tricks: Add External URLs to Print Stylesheets
CSS Tips and Tricks: Add External URLs to Print Stylesheets
Corey Schafer
16 JavaScript Arrays: Properties, Methods, and Manipulation (Part 7 of 7)
JavaScript Arrays: Properties, Methods, and Manipulation (Part 7 of 7)
Corey Schafer
17 JavaScript Arrays: Properties, Methods, and Manipulation (Part 1 of 7)
JavaScript Arrays: Properties, Methods, and Manipulation (Part 1 of 7)
Corey Schafer
18 JavaScript Arrays: Properties, Methods, and Manipulation (Part 5 of 7)
JavaScript Arrays: Properties, Methods, and Manipulation (Part 5 of 7)
Corey Schafer
19 JavaScript Arrays: Properties, Methods, and Manipulation (Part 4 of 7)
JavaScript Arrays: Properties, Methods, and Manipulation (Part 4 of 7)
Corey Schafer
20 JavaScript Arrays: Properties, Methods, and Manipulation (Part 3 of 7)
JavaScript Arrays: Properties, Methods, and Manipulation (Part 3 of 7)
Corey Schafer
21 JavaScript Arrays: Properties, Methods, and Manipulation (Part 2 of 7)
JavaScript Arrays: Properties, Methods, and Manipulation (Part 2 of 7)
Corey Schafer
JavaScript Arrays: Properties, Methods, and Manipulation (Part 6 of 7)
JavaScript Arrays: Properties, Methods, and Manipulation (Part 6 of 7)
Corey Schafer
23 Python Tutorial: if __name__ == '__main__'
Python Tutorial: if __name__ == '__main__'
Corey Schafer
24 Sublime Text Quick Tip: "Go To Definition" Click Shortcut
Sublime Text Quick Tip: "Go To Definition" Click Shortcut
Corey Schafer
25 How to quickly create favicons for the desktop, Apple/Android devices, tablets, and more
How to quickly create favicons for the desktop, Apple/Android devices, tablets, and more
Corey Schafer
26 Easily Resize Multiple Images Using Picasa
Easily Resize Multiple Images Using Picasa
Corey Schafer
27 Easily Resize Multiple Images Using the Mac Terminal
Easily Resize Multiple Images Using the Mac Terminal
Corey Schafer
28 Python Tutorial: virtualenv and why you should use virtual environments
Python Tutorial: virtualenv and why you should use virtual environments
Corey Schafer
29 Python Tutorial: pip - An in-depth look at the package management system
Python Tutorial: pip - An in-depth look at the package management system
Corey Schafer
30 Git Tutorial: Using the Stash Command
Git Tutorial: Using the Stash Command
Corey Schafer
31 How Software Engineers, Developers, and Designers can volunteer their skills
How Software Engineers, Developers, and Designers can volunteer their skills
Corey Schafer
32 Git Tutorial: Diff and Merge Tools
Git Tutorial: Diff and Merge Tools
Corey Schafer
33 Git Tutorial: Change DiffMerge Font-Size on Mac OSX
Git Tutorial: Change DiffMerge Font-Size on Mac OSX
Corey Schafer
34 Sublime Text Quick Tip: Launch Sublime Text from the Terminal
Sublime Text Quick Tip: Launch Sublime Text from the Terminal
Corey Schafer
35 Python Tutorial: str() vs repr()
Python Tutorial: str() vs repr()
Corey Schafer
36 Programming Terms: DRY (Don't Repeat Yourself)
Programming Terms: DRY (Don't Repeat Yourself)
Corey Schafer
37 Programming Terms: String Interpolation
Programming Terms: String Interpolation
Corey Schafer
38 Programming Terms: Idempotence
Programming Terms: Idempotence
Corey Schafer
39 Python Tutorial: Namedtuple - When and why should you use namedtuples?
Python Tutorial: Namedtuple - When and why should you use namedtuples?
Corey Schafer
40 Programming Terms: Mutable vs Immutable
Programming Terms: Mutable vs Immutable
Corey Schafer
41 Python Tutorial: Else Clauses on Loops
Python Tutorial: Else Clauses on Loops
Corey Schafer
42 Overview of Online Learning Resources
Overview of Online Learning Resources
Corey Schafer
43 Mac OS X Terminal Tutorial: Time-Saving Keyboard Shortcuts
Mac OS X Terminal Tutorial: Time-Saving Keyboard Shortcuts
Corey Schafer
44 Git Tutorial for Beginners: Command-Line Fundamentals
Git Tutorial for Beginners: Command-Line Fundamentals
Corey Schafer
45 Quickest and Easiest Way to Run a Local Web-Server
Quickest and Easiest Way to Run a Local Web-Server
Corey Schafer
46 Python Tutorial: Generators - How to use them and the benefits you receive
Python Tutorial: Generators - How to use them and the benefits you receive
Corey Schafer
47 Python Tutorial: Comprehensions - How they work and why you should be using them
Python Tutorial: Comprehensions - How they work and why you should be using them
Corey Schafer
48 Chrome Quick Tip: Quickly Bookmark Open Tabs for Later Viewing
Chrome Quick Tip: Quickly Bookmark Open Tabs for Later Viewing
Corey Schafer
49 Programming Terms: Combinations and Permutations
Programming Terms: Combinations and Permutations
Corey Schafer
50 Git Tutorial: Difference between "add -A", "add -u", "add .", and "add *"
Git Tutorial: Difference between "add -A", "add -u", "add .", and "add *"
Corey Schafer
51 Preparing for a Python Interview: 10 Things You Should Know
Preparing for a Python Interview: 10 Things You Should Know
Corey Schafer
52 SQL Tutorial for Beginners 1: Installing PostgreSQL and Creating Your First Database
SQL Tutorial for Beginners 1: Installing PostgreSQL and Creating Your First Database
Corey Schafer
53 SQL Tutorial for Beginners 2: Creating Your First Table
SQL Tutorial for Beginners 2: Creating Your First Table
Corey Schafer
54 SQL Tutorial for Beginners 3: INSERT - Adding Records to Your Database
SQL Tutorial for Beginners 3: INSERT - Adding Records to Your Database
Corey Schafer
55 Linux/Mac Terminal Tutorial: Navigating your Filesystem
Linux/Mac Terminal Tutorial: Navigating your Filesystem
Corey Schafer
56 Python: Ex Machina Easter Egg - Hidden Message within the Code
Python: Ex Machina Easter Egg - Hidden Message within the Code
Corey Schafer
57 Mac Tip: New Split Screen Feature in El Capitan
Mac Tip: New Split Screen Feature in El Capitan
Corey Schafer
58 Setting up a Python Development Environment in Eclipse
Setting up a Python Development Environment in Eclipse
Corey Schafer
59 Git Tutorial: Fixing Common Mistakes and Undoing Bad Commits
Git Tutorial: Fixing Common Mistakes and Undoing Bad Commits
Corey Schafer
60 SQL Tutorial for Beginners 4: SELECT - Retrieving Records from Your Database
SQL Tutorial for Beginners 4: SELECT - Retrieving Records from Your Database
Corey Schafer

This video teaches how to use the reduce() and reduceRight() methods in JavaScript arrays for data manipulation and processing, covering concepts such as initial values, array methods, and functional programming.

Key Takeaways
  1. Uncomment code to see the reduced result
  2. Run reduce() on an array of characters
  3. Run reduceRight() on an array of characters
  4. Pass an initial value of 0 to reduce() for objects
  5. Run the reduce method on the array with the initial value of 0
  6. Assign the return value of previous to previous on the next call
💡 The reduce() method can be used to process data in JavaScript arrays by applying a function to each element and reducing it to a single output value, while the reduceRight() method does the same but from right to left.

Related AI Lessons

Had my Frontend Developer interview with Capgemini (Application Developer) today, and I wanted to…
Prepare for a frontend developer interview with Capgemini by reviewing JavaScript fundamentals and practicing common interview questions
Medium · JavaScript
10 Frontend Developer Tools to Boost Productivity in 2026
Boost frontend productivity with 10 essential tools for modern web app development
Medium · Programming
10 Frontend Developer Tools to Boost Productivity in 2026
Boost frontend productivity with top 10 developer tools in 2026
Medium · JavaScript
The US Frontend Engineer Market in 2026: A Data-Driven Reality Check (and the Bias That Stops Us Seeing It)
US frontend engineer hiring demand peaked in 2022 and remains flat-depressed in 2026, contrary to common assumptions
Dev.to AI
Up next
The masks we wear | Zora Krstić | TEDxLuxembourgCity
TEDx Talks
Watch →