Javascript hoisting with variable and function [duplicate] - javascript

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);

Related

Closure function using outer variable instead of nearest variable [duplicate]

This question already has answers here:
How do JavaScript closures work?
(86 answers)
What is the scope of variables in JavaScript?
(27 answers)
Closed last month.
I don't understand why the functions are taking the outer scoped variable instead of the nearest one when called as callback.
function outerFn(){
let x = 1;
function log(){
console.log(x);
};
function run(fn){
let x = 100;
fn();
}
run(log);
};
outerFn();
I was expecting the run to log 100 instead of 1.
"x" is being closed over by your "log()" function. Your "log()" function does not have access to "x" declared in "run()".

Is there a way to use a variable inside an arrow function but initialize it inside another function where the arrow function is also called? [duplicate]

This question already has answers here:
What is the scope of variables in JavaScript?
(27 answers)
What is lexical scope?
(21 answers)
Closed 10 months ago.
I'm trying to access the variable c that is initialized inside myFunc when I call sum.
let sum = (a,b) => {
let result = a + b + c;
return result;
}
function myFunc(){
let c = 5;
return sum(1,2);
}
console.log(myFunc());
But I'm getting an error because of the c that I use inside the arrow function. Don't know if I'm explaining right but I think you get the point by seeing the code above.

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.

How to change the value of a passed variable in a function in javascript? [duplicate]

This question already has answers here:
Is JavaScript a pass-by-reference or pass-by-value language?
(33 answers)
Closed 3 years ago.
Inside a function, I'd like to change the value of a global variable that is specified by the function's argument.
A = 1;
B = 2;
C = 3;
function myFunction(variable) {
variable += 10;
}
myFunction(A);
myFunction(B);
myFunction(C);
I'm looking for these results:
console.log(A); // 11 expected
console.log(B); // 12 expected
console.log(C); // 13 expected
I can't output the new value via a return statement because it's computed inside the callback function of a post request.
How to achieve this?
It's not possible. This is called shadowing, that's that your argument is other variable than global, so A in myFunction is other that A in global
Maybe trying to run with apply() and using this.variable inside function would help

Explaining anonymous function [duplicate]

This question already has answers here:
What is the (function() { } )() construct in JavaScript?
(28 answers)
Advanced JavaScript: Why is this function wrapped in parentheses? [duplicate]
(4 answers)
Closed 4 years ago.
Can anybody explain below code? All I know is it is anonymous function but what is (0) doing?
var output = (function(x) {
delete x;
return x;
})(0);
console.log(output);
Why output of above code comes zero. Can anybody explain?
That's because what you're doing is creating a function and then calling it immediately where x = 0. Your function is returning x, and thus 0.
As for what anonymous functions are, they are basically a function that gets stored in a variable. You call it from the variable instead of by name. So, for example:
var output = function (x) { return x;};
Can be called like this:
output(0);
As opposed to the normal way like this:
function myOutput(x) {
return x;
}
myOutput(0);

Categories