📚 Error — Experimenting With Scope

I made my own function to experiment with why we use return, but am getting an error when I try to write it a different way.

I suppose I should also provide a little bit of background context; let’s say that I wanted to make a site that finds books people want to read. The user would type in the book’s title and that author, and while the website is loading, it will take the user to a page that says it’s looking for the book.

Below is a codebyte with the first way I wrote my code, which works like how I expected it to; the output says it is looking for a book by its specific author. (The example in the code is Looking for "Divergent" by Veronica Roth... )

HOWEVER, I also wrote my code a second way — without using return — and it runs perfectly fine. So I tried to find out how return was important to my first example…

// RUNS function findBooks(searchAuthor = 'your author', searchTitle = 'your book') { console.log('Let’s get started! Type in the author and book title.'); var bookAuthor = searchAuthor; var bookTitle = searchTitle; return console.log(`Looking for "${bookTitle}" by ${bookAuthor}...`); } findBooks('Veronica Roth', 'Divergent'); // ALSO RUNS function findBooks(author, title) { console.log('Let’s get started! Type in the author and book title.'); console.log(`Looking for "${title}" by ${author}...`); } findBooks('Patrick Ness', 'The Wide, Wide Sea');

…by using the variables outside of my function in this third example. Which obviously didn’t work, because (according to my understanding, anyway) we cannot treat parameters as variables outside of the function. That made sense, but the thing is, I can’t seem to figure out a way to make it work. So my question is, how can I use these variables outside my function to make them work? And well… the bigger question, really… how is using return important to this specific example? (As we’ve seen, the output is the same for both the above ways…)

// DOESN'T run function findBooks(searchAuthor = 'your author', searchTitle = 'your book') { console.log('Let’s get started! Type in the author and book title.'); return console.log(`Looking for "${bookTitle}" by ${bookAuthor}...`); } var bookAuthor = searchAuthor; var bookTitle = searchTitle; findBooks('Veronica Roth', 'Divergent');

OKAY I admit this was one long post. But now understanding this concept (and my own example LOL :rofl:) has really intrigued me, and I just can’t rest… what I’m saying is, THANK YOU, reader, for reading all of this, and if you take the time to answer my questions and attempt to make me understand… well, I have a million more thank you’s. :pleading_face: :wine_glass: :wine_glass:

we can only return one thing, this one thing can of course be an array or object containing multiple values

.log() is also a function (actually, its a method, but doesn’t matter), given the purpose of this function/method is to log to the console, the .log method does not have a return value, which results in undefined

if for whatever reason you need the parameters outside the function, you should simply assign the values to a variable before hand:

var bookAuthor = 'Veronica Roth';
var bookTitle = 'Divergent';
findBooks(bookAuthor, bookTitle);

return is useful to hand back the result of the function

This is… wow… actually, much more simple than I thought haha :sweat_smile: If the values are based on user inputs though, and we want to store that in a variable… well I think that is a bit more advanced than where I currently stand :laughing: , but I can definitely see how we would use the arrays and objects you mentioned!

All this to say, thank you so much for your explanation! It was a bit confusing at first, when I played around with the code myself, but this concept is clear to me now. Thank you again!

1 Like