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 trialorange sky
Front End Web Development Techdegree Student 4,945 Points['there', 'are', true, function(a){return a}]
Hello!
Does anybody understand why the instructor teaches that when we add a boolean, or a function in an array, it returns the toString value of them. He slighly mentions it toward the end of 'methods 3 on Array' video lecture. He said this information would come in handy later.
I cant understand the overall purpose of understanding that in the code below the true and function return a toString value. I am pretty sure he is heading toward something very important, but I am failing to connect the dots.
<!DOCTYPE html>
<html lang="en">
<head>
<title> JavaScript Foundations: Arrays</title>
<style>
html {
background: #FAFAFA;
font-family: sans-serif;
}
</style>
</head>
<body>
<h1>JavaScript Foundations</h1>
<h2>Arrays: Methods Part 1</h2>
<script>
var a = ["these", "are" , " some" , "words", true, function(a){return a}];
var b = a.join(' ');
</script>
</body>
</html>
4 Answers
Andrew Kiernan
26,892 PointsI just watched the video again to refresh my memory. I agree it is tough to understand what is being said. When you store data types in an array they exist as that data type. When you console.log() the array those elements should still exist as the original data type.
When Jim mentions the part that "it will come in handy when we work with our own objects" I think what he's referring to is that when you create your own object, you can essentially do whatever you want with them, including overwriting the toString() method (you can do this with all data types, but it is generally not a good idea). So you can write your own toString() method for functions that you write, which when you do something like call Array.join(), you'll be able to manipulate the string that you boolean or function returns.
The main takeaway from it is that when you work with joining and manipulating arrays of different data types, the way Javascript makes it all work is to call the toString() method on each element, and knowing this you can overwrite the method for certain data types to output a string that you want.
I hope that helps!
Andrew Kiernan
26,892 PointsHello!
In the code example, knowing that the boolean and function will be called with toString() is necessary to know why b = a.join(' ') will work. In Javascript (and I think most, if not all other languages) you cannot perform operations on different data types. Javascript's solution to this is to call the toString() method on data types that are not strings (it could be called on strings too I suppose, but it wouldn't change anything, still someone correct me if I'm wrong!).
For example, if you set var a = 5 (the number) and var b = "5" (string of the number 5), the result of a + b is "55", because Javascript will convert the non-string into a string, thus the + operator will concatenate them, giving you "55". If you wanted to do math with these variables, you would need to do a + parseInt(b) to turn b into a number, which will yield a + b = 10.
I hope I didn't make that too incoherent! If you have any questions just let me know!
orange sky
Front End Web Development Techdegree Student 4,945 PointsHello Andrew,
I understand what you are saying, but the video is hmm talking about somthing different. They are not talking about calling a toString() on an array, the way we do when we want to console.log(myArray.toString()). Above, the array is storing [ true, function(){..}] like objects but when you console.log the array, it returns the string version of these 2 objects . For primitive data like numbers and strings, I can see how they come out as Strings but for objects like a function or a boolean value??(maybe an object), yeah I am not sure. Have a look at the vid, and you will see what I mean. Anyway, thank you for your response, but I will just take a mental that they become strings when we output the values in an array.
orange sky
Front End Web Development Techdegree Student 4,945 Pointsthanks Andrew!