JavaScript Function within a function undefined [duplicate] - javascript

This question already has answers here:
What is the scope of variables in JavaScript?
(27 answers)
Closed 6 years ago.
Can someone explain to me why the second function within the first function is undefined?
var a = 1
function abc () {
alert(a);
function xyz () {
alert(a);
}
}
https://jsfiddle.net/kp950/yLs73cth/

xyz is an inner function which is private to abc function.
You cannot call xyz unless you make it public

This is due to the scope at which you are trying to execute xyz() (globally). xyz() can only be run inside of abc() - where it is defined in the local scope.

Related

Javascript hoisting with variable and function [duplicate]

This question already has answers here:
Javascript - Precedence in hoisting
(2 answers)
Closed 7 months ago.
var a=2;
function a(){};
console.log(typeof(a));
I am wondering why the result of the above code is number. I know the var is hoisted but isn't it already at the first line of the code? so why the type of a is still a number instead of a function?
In this case function a(){} is actually hoisted, which you can see if you log a at various points:
console.log(a);
var a = 2;
console.log(a);
function a() {
console.log('hi');
}
console.log(a);

Why the function's log is the function itself? [duplicate]

This question already has an answer here:
Why is the named IIFE logged instead of the variable with the same name?
(1 answer)
Closed 2 years ago.
I have encountered a problem that if the IFEE function's name is the same with variable's name in it. the output is the function itself. Why?
var b = 10;
(function b() {
b = 20;
console.log(b);
})();
Named function expressions create a read only variable in their own scope which matches their name and references themselves.
This is useful for writing recursive functions.

Is there a way to declare and instantly run function? [duplicate]

This question already has answers here:
What is the (function() { } )() construct in JavaScript?
(28 answers)
Closed 6 years ago.
I've a code:
var myFn = function(){
//some code
}
myFn();
So I have to define a function, then run it in two rows.
Is there a way to define a function (with storage it in variable) and run it instantly, in one expression? Just a short way of this.
var myFn = function(){
console.log("test");
}()
myFn will be undefined because the function doesn't return anything

Javascript ( function () { ... } ) (); [duplicate]

This question already has answers here:
What is the (function() { } )() construct in JavaScript?
(28 answers)
How do JavaScript closures work?
(86 answers)
Closed 7 years ago.
Hello I've got a function like this one.
function doSomething () {
( function () {
// callbacks and code
}
)();
}
I am not use to that typing, I'm looking forward to understand how is interpreted by the browser or what does it mean the ( function () ) ();
Thought I know the differences between calls and assigns , I can't read this properly , surely is something simple but I don't really get it.
Thanks.
Is a self invoked anonymous function:
A self-invoking anonymous runs automatically/immediately when you create it and has no name, hence called anonymous. Here is the format of self-executing anonymous function:
(function(){
// some codeā€¦
})();
https://sarfraznawaz.wordpress.com/2012/01/26/javascript-self-invoking-functions/

Difference between function myName() and var myName = function() in javascript? [duplicate]

This question already has answers here:
var functionName = function() {} vs function functionName() {}
(41 answers)
Closed 9 years ago.
Is there any difference between declaring a Javascript function like this:
function myName(...)
and like this:
var myName = function(...)
I don't believe so, but...
The first is a function declaration
You have given it a name.
It will be hoisted.
The second is a function expression
The way you wrote it, it's anonymous.
It will only be available after the line where it is defined.

Categories