InterviewSolution
| 1. |
When an onfocus event is triggered in JavaScript? |
|
Answer» Function declaration is like most other traditional languages, but in JavaScript we use the keyword “function”. One of the key difference is that, we can call a function declaration even before defining it but same is not true for function expression and it will give reference error. Let’s move both the function call to the top. console.log(funcDeclaration()); //Function declaration console.log(funcExpression()); //ReferenceError function funcDeclaration() { console.log('Function declaration'); } let funcExpression = function() { console.log('Function expression'); }/* Exception: ReferenceError: can't access lexical declaration `funcExpression' before initialization @Scratchpad/7:2:1 */ But why did we got this Reference error in function expression call. This is to do with the compiler and interpreter step in JavaScript, which we UNDERSTOOD in Question 1. |
|