This question already has answers here:
What is the scope of variables in JavaScript?
(27 answers)
Closed 6 years ago.
I have searched the internet trying to find a good answer for this question. What I mainly found is suggestions to move the variable in the global scope, or using the functions as parameters for the function I want to use the variable in, but without explanation on how that works
To explain my dilema, lets say we have this piece of code:
function foo(){
var x = 2;
}
function bar(){
var z = 2;
}
function compare(foo,bar){
if ( z === x ) {
console.log("text");
}
}
This is the problem I'm facing. Is the code I've written above correct and if I call the compare() function it should console log "text" ?
declare with globel variable it's will easy to pass Another function
var x;
var y;
function foo(){
x = 2;
}
function bar(){
z = 2;
}
function compare(){
if ( z === x ) {
console.log("text");
}
}
foo()
bar()
compare();
Related
This question already has answers here:
Why use named function expressions?
(5 answers)
Closed 4 years ago.
Consider the following snippet:
let myFunc = function foo() {
console.log('whatever');
}
myFunc(); // 'whatever'
foo(); // ReferenceError
What reason is there to give this function a name if you can't use it?
A named function is to call itself with this given name. The name can not changed later.
For example this function is calling itself only two times.
let myFunc = function foo() {
console.log('whatever');
foo.count = (foo.count || 0) + 1;
if (foo.count < 3) foo();
}
myFunc();
This question already has answers here:
What is the scope of variables in JavaScript?
(27 answers)
Closed 4 years ago.
function x() {
var a = 2;
y();
}
function y() {
a += 3;
}
x();
//returns a is not defined
If i declare a function (y) outside the function (x) where it's been called, why it isn't inherit the outer function's variables and values? There's a way to solve the problem described above?
Variable a is declared as part of the function x(). That's why a is available only inside x(). To solve the problem you have to pass variable a as the function parameter:
function x() {
var a = 2;
y(a);
}
function y(a) {
a += 3;
console.log(a);
}
x();
This question already has answers here:
Can you clarify this JavaScript variable hoisting behavior?
(3 answers)
Closed 5 years ago.
var x = 3;
function func(randomize) {
if (randomize) {
var x = Math.random();
return x;
}
return x;
}
console.log(func(false));
As you can see from above code, if statement never be true but the x value is undefined, i would like to understand how variable declaration works in javascript. Any references also be helpful.
Update:
Can anybody explain why variable was re-declared to undefined and how it is related to hoisting in javascript
Because func is in essence:
function func(randomize) {
var x;
if (randomize) {
x = Math.random();
return x;
}
return x;
}
Variables in JavaScript have function scope, not block scope with which you might be familiar from other languages. This results in the hoisting behavior you observe:
What does happen is that variable and function declarations are put into memory during the compile phase, but stays exactly where you typed it in your coding.
...
JavaScript only hoists declarations, not initializations. (emphases mine)
That is, even though inside func you declare x in the if block, during compile time its declaration is moved to function level where it shadows x in global scope. However, it is only initialized if the argument to func is true which leads to the behavior you observe.
See also let:
"use strict";
var x = 3;
function func(randomize) {
if (randomize) {
let x = Math.random();
return x;
}
return x;
}
console.log(func(false));
console.log(func(true));
You define a new variable var x in your function and thus overwrite the outer variable x.
This question already has answers here:
var functionName = function() {} vs function functionName() {}
(41 answers)
Closed 9 years ago.
I was told that you can declare functions in JavaScript more than 1 way.
ex.
// One way
function sqrt(x){
return x * x;
}
// Second way
var sqrtAlt = function (x){
return x * x;
}
What is the difference between these two function declarations?
The output is same but must have a reason to have two ways?
I am also curious about how you would use them.
Lastly, are there any other ways?
Thanks.
When you are defining
function sqrt(x){
return x * x;
}
is that the function name appears in Firebug debugger.
Functions that are declared as
var sqrtAlt = function (x){
return x * x;
}
come up as anonymous.
Also check out this Thread
They are basically the same thing, but in the second example you additionally assign the function to a variable. This way of creating a function is very useful when overriding an existing function of some object, let's say:
window.alert = function(text)
{
// Do something ...
};
This question already has answers here:
What is the scope of variables in JavaScript?
(27 answers)
Closed 8 years ago.
function t()
{
var x = 1;
if(true)
{
var x = 2;
alert(x);
}
alert(x);
}
t();
Anyone knows the reason?
Because JavaScript (well ECMAScript) does not have block scope (yet). Just function scope.
There's really just one variable declaration that is hoisted to the top of the function, so x=2 is overwriting the initial value of 1.
function t()
{
var x = 1;
// v---------immediately invoked function to create a new scope
(function() {
// new variable scope
if(true)
{
var x = 2;
alert(x); // 2
}
})();
alert(x); // 1
}
t();
The 'var' keyword applies within a whole function, so the code you have will behave identically to this:
function t() {
var x = 1;
if (true) {
x = 2;
alert(x);
}
alert(x);
}
t();
Variables in Javascript are scoped to the function, not to blocks. You've got two vars, but there's really only one x.