This forum is now read-only. Please use our new forums! Go to forums
Question about Function Declarations (3/6) and (4/6)
In exercise 3/6, we create a function called printPerson()
that looks roughly like this:
var printPerson = function(person) {
console.log(person.firstName + " " + person.lastName);
}
After that exercise is done and we move on to the next function, printPerson()
is then changed to this:
function printPerson(person) {
console.log(person.firstName + " " + person.lastName);
}
Does anyone know why the declaration is changed? I kinda-sorta tried to figure it out by myself by calling the printPerson()
function using the new declaration, but a (I think) Reference error was thrown.
Any input would be appreciated, but please try not to go waaay over my head :-\ .
Answer 53d48ef0631fe9039b0012aa
Unless we had the author to ask this question of, an answer as to why is, well, out of the question. The two forms are functionally the same under correct conditions.
The first condition would be the existence of defined values for the indentifierNames, person.firstName
and person.lastName
.
The second and perhaps the more important, the function expression must be defined before any reference to it. Consider the following:
doSum(17,25);
function doSum(a,b){
return a+b;
}
// 42
but this,
doProduct(6,7);
var doProduct = function(u,v){
return u * v;
}
// TypeError: undefined is not a function
A term to study while exploring functions is hoisting. Here I leave you to continue your research…
Popular free courses
- Free Course
Learn SQL
In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.Beginner friendly,4 LessonsLanguage Fluency - Free Course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner friendly,11 LessonsLanguage Fluency - Free Course
Learn HTML
Start at the beginning by learning HTML basics — an important foundation for building and editing web pages.Beginner friendly,6 LessonsLanguage Fluency
1 comments
Thanks, I’ll look up and hopefully make more sense of it.