Invoking function declaration in JavaScript [duplicate] - javascript

This question already has answers here:
What are the precise semantics of block-level functions in ES6?
(2 answers)
Function declaration in block moving temporary value outside of block?
(1 answer)
Closed 7 months ago.
I have a doubt regarding how the function declaration are invoked in JavaScript.
I have read somewhere that function declaration can be accessed anywhere within the function it was declared on.
Lets say I declare a function within a block of code so it will be accessible anywhere or maybe outside the block as well.
But when I try to invoke the function before the block of code, I get an TypeError. But this error is not happening when I invoke the function after the block of code. Please explain why the function is not getting invoked before the block of code.
function globalFunc() {
//..
// ..
funcName(); // not accessible - TypeError
{
function funcName() {
console.log("Hey");
}
}
funcName(); // accessible
}
globalFunc();

Related

Calling an immediately invoked function expression (IIFE) in Javascript [duplicate]

This question already has answers here:
Explain the encapsulated anonymous function syntax
(10 answers)
Location of parenthesis for auto-executing anonymous JavaScript functions?
(4 answers)
strange observation on IIFE in node.js (Windows)
(2 answers)
Closed 15 days ago.
In Javascript, I can define and call an IIFE as such:
(function() {
console.log("foo");
})();
As I understand it, this defines an anonymous function (thus the parentheses surrounding the function definition) and then immediately invokes it (thus the following parentheses, ()).
I noticed that the following also produces the same result:
(function() {
console.log("foo");
}());
Why is that? If I were to skip the parentheses surrounding the entire definition, it would cause an error:
function() {
console.log("foo");
}();
This results in SyntaxError: Function statements require a function name. How does the interpreter understand the second definition and why does it work?

What the heck is going on in Javascript scopes and hoisting? [duplicate]

This question already has answers here:
Function declarations inside if/else statements?
(4 answers)
What are the precise semantics of block-level functions in ES6?
(2 answers)
Closed 11 months ago.
Based on my current knowledge, I faced something really unexpected in Javascript.
Consider this simple code:
foo();
if(true){
function foo(){
console.log(i);
}
}
foo();
I know that variable and function declarations will be hoisted by the Js engine at the creation phase. In this example the function foo will be hoisted and when creation phase is done. The engine will start to run the code line by line, at the first line of the code a function called foo should be invoked. We have that function, because it was hoisted before right? the problem is here a TypeError will be thrown which says that foo is not a function.
And when I remove the if block everything works well. I think it's somehow related to the block scopes but don't know how exactly and why it behaves like this.
What's going on here?
I know that function declarations should not be placed in blocks.

Difference between function declerations [duplicate]

This question already has answers here:
var functionName = function() {} vs function functionName() {}
(41 answers)
Closed 2 years ago.
What is the difference between this (a 'standalone' function):
function standaloneFunction () {
console.log('standaloneFunction runs, success')
}
standaloneFunction()
...and this (a function inside a variable):
let variableFunction = function variableFunction () {
console.log('function inside let has same name as the variable, runs? - yep, does.')
}
variableFunction()
Is it a problem that the variable and the function share the same name?
it doesnt seem so - i speculate this is because it has something to do how variables
and functions are saved in memory? Functions in their entirety, and variables only their declaration?
When i do console.log(this), i can't find the 'variableFunction' in the execution
context of 'this' - however, 'standaloneFunction' does appear.
Have you, as a beginner, also asked yourself such questions? Am i being too picky about such details?
Should i already use es6 syntax?
Please also don't hold back with any advice regarding articulating my question.
Thanks to everyone who has taken their time to read this.
The first is a function declaration, which will be hoisted. The second is a named function expression, but I think an anonymous function expression would be better in this case since there’s no need to name the function, e.g. let variableFunction = function() {…}.
Please see What is the difference between a function expression vs declaration in JavaScript?

call self invoking function from outside body function [duplicate]

This question already has an answer here:
Function in if condition clause
(1 answer)
Closed 6 years ago.
So i have the following code:
(function init_chart(){
// body function
})();
it's working as expected. but when i tried to call the init_chart() from outside the function, console said it is undefined. then i tried to add the following line inside init_chart() function:
window['init_chart'] = this;
now console said init_chart is not a function.
You are creating a named function expression function, so basically the function is not defined on the global scope. The easiest way to get around it is to do the following:
function init_chart(){
// body function
};
init_chart();

difference between javascript function declaration [duplicate]

This question already has an answer here:
Anonymous function vs normal function
(1 answer)
Closed 9 years ago.
I know this type of question gets asked a lot, but I haven't seen any question with this type of declaration
(function(){
myFuncName=function(myVar){
// some logic
};
}());
how does that differ from
function myFuncName(myVar){
// some logic
}
The first one is an anonymous function and there is no way you can reference to and call it later on, so you just executes instantly ( ) once it had been created!
(function(){
alert(1)
}())
The second one is a reference function that you can call it anytime later. It wont gets executed unless you call it explicitly
What your doing is creating an anonymous function that also has a closure in it. Read more about closures here.
Basically a closure means that you have declared a function inside of another function. Which will let you access local variables after the first function exits.
Normally you would not be able to do this since they would be out of scope.
As for the other part. You can find a very useful guide to what's happening here Why do you need to invoke an anonymous function on the same line?.
To sum it up though, you have created an anonymous self-invoking function expression.
The self-invoking comes from the fact that () immediately follows the function expression.
Here's a link which explains the the function declarations in javascript.

Categories