Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialPeter Huang
5,427 Pointsif map() the same as join()?
why does map and join look the same use?
2 Answers
Bella Bradbury
Front End Web Development Techdegree Graduate 32,790 PointsHi Peter!
These might look the same upon first glance, but they're actually quite different. Let's break them both down and then compare them with an example!
The join()
method creates a concatenated string from an array. This means that it takes all of the separate elements of an array and combines them into just one string. The information is no longer separated. Here's a link to join() documentation.
The map()
method creates a new array based off of a different array. This means that the information is still separate and should be transformed in some way. Here's a link to map() documentation.
Let's use an example to really highlight the differences!
let initialArray = [50, 30, 900, 60, 80];
function joinArray() {
let stringArray = initialArray.join();
console.log(stringArray);
// console will log "50, 30, 900, 60, 80" as a STRING
// this is one value and not actually separate numbers
}
joinArray();
function mapArray() {
let newArray = initialArray.map(x => x / 10);
console.log(newArray);
// console will log [5, 3, 90, 6, 8] an an ARRAY
// these are still separate values and is still iterable
}
mapArray();
Hopefully this helps with the difference!
Peter Huang
5,427 PointsThank you so much!!!