Hello everyone,
I'm relatively new to programming, and I've been practicing functions and variables, mainly for web development. However, I'm still trying to grasp the concept fully. I've watched several tutorials, and I've noticed that some use functions within a variable while others use functions alone. I understand the idea of reusing functions multiple times, but at times, it seems unnecessary. Let's take an example to illustrate my point:
Example 1 :
var askQuestion = function() {
return prompt('What\'s the first letter of your name?');
}
Example 2:
function askQuestion() {
return prompt('What\'s the first letter of your name?');
}
Both examples seem to output the same thing. If I want to reuse the function later on, I still have to refer to it as askQuestion or askQuestion() in both cases, right? The only difference I notice is the absence of parentheses in the first example. So, my question is, what is the point of assigning the function to a variable? I'm still a beginner, and I might be missing something obvious. I'd greatly appreciate any help or clarification you can provide. Thank you! :)
I have tried watching tutorials and finding explanations online...but they all say the same thing..about reusing the function several times....but nobody seems to talk in detail about why one is more preferred over the other depending on use case scenarios...I would like a response in detail with explanations as to why.