LOGIN
Ali Stone
9
Top Contributor
Official Community Moderator (Decision maker on who becomes Verified Freelance Talent)
 · New YorkU.S.
Share
Report
Get Link
QUESTION
Open

Question posted by collaborizm workspace (slackbot)

Need help on a javascript quiz…

1 like 
Like
Award Contribution
Load 4 previous comments
Ali Stone
9
Top Contributor
Official Community Moderator (Decision maker on who becomes Verified Freelance Talent)
 · New YorkU.S.
Share
Report
Get Link

what about the scond question though?

Like
0 like 
Award Contribution
Ranjan Dailata
92
Community Expert
Solution Architect. Collaborizm Roundtable.
 · MysuruIndia
Share
Report
Get Link

Sorry about that. I missed your second question. The error that you are getting is right - log is not a function. That’s because it’s a function expression. Welcome to Javascript 😃

You can’t use function expressions before you define them.

Refer - https://developer.mozilla.org/en-US/docs/web/JavaScript/Reference/Operators/function

https://stackoverflow.com/questions/336859/var-functionname-function-vs-function-functionname

https://medium.com/@mandeep1012/function-declarations-vs-function-expressions-b43646042052

Like
0 like 
Award Contribution
Ali Stone
9
Top Contributor
Official Community Moderator (Decision maker on who becomes Verified Freelance Talent)
 · New YorkU.S.
Share
Report
Get Link

but then why could you use the function expression in question 1?
wouldn’t it just be undefined again?

Like
0 like 
Award Contribution
Ranjan Dailata
92
Community Expert
Solution Architect. Collaborizm Roundtable.
 · MysuruIndia
Share
Report
Get Link

The first one being a function that’s declared, doesn’t matter whether you do it first or not. But the second approach is a function expression created using var, much like an ordinary variable being created. This is the case of a function reference stored within the variable. Hence you cannot call these ahead of the time as you are creating the function expression at the later point.

Like
0 like 
Award Contribution
Ali Stone
9
Top Contributor
Official Community Moderator (Decision maker on who becomes Verified Freelance Talent)
 · New YorkU.S.
Share
Report
Get Link

but in question one, the function is called BEFORE it’s declared too…that’s why it’s undefined?

Like
0 like 
Award Contribution
Ranjan Dailata
92
Community Expert
Solution Architect. Collaborizm Roundtable.
 · MysuruIndia
Share
Report
Get Link

The undefined log that you are seeing is because of the variable not defined and initialized at first. It’s not same as not a function. Although the code looks almost same but they are not. It works in a totally different way as they are written.

console.log(greeting);
var greeting = ‘Hello!’;

Like
1 like 
Award Contribution
Leave a reply...
DISCOVER
CHAT
HIRE
ACTIVITY
FEED
Chat with us!
Help
Write something before you submit it!
Photo updated
Request Sent!
Updated
Copied to Clipboard