Scopes at functions? [duplicate] - javascript

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

Related

Javascript: why a variable undefined in inner function, even though the variable declared and assigned in the global [duplicate]

This question already has answers here:
Surprised that global variable has undefined value in JavaScript
(6 answers)
What is the scope of variables in JavaScript?
(27 answers)
Closed 2 years ago.
let a = 1;
function outer() {
console.log(a);
function inner() {
console.log(a);
var a = 3;
}
inner();
console.log(a);
}
outer();
why 'a' is undefined in the inner function above? shouldn't it be able to find a = 1 in the first line through scope chain?
inner function is inside outer function, and outer function is inside the global scope.
It is because of Javascript hoisting
Your inner() function is equal to -
function inner() {
var a;
console.log(a);
a = 3;
}
which would obviously output undefined in the console.
It has to do with the scope, here is a way how you can do this..
let a = 1;
function outer() {
console.log(a);
let newA = a
function inner() {
console.log(newA);
}
inner();
console.log(a);
}
outer();

Accessing a local variable of a function from another function [duplicate]

This question already has answers here:
Accessing variables from other functions without using global variables
(10 answers)
Closed 5 years ago.
Using the following code how can I access a from inside log2?
(function() {
function log() {
var a = "string";
}
log()
function log2() {
console.log(log.a);
}
console.log(log);
log2();
})()
Variables declared with the var keyword in JavaScript are function-scoped. This means that they cannot be accessed from outside the function they were declared in without returning them. The solution is to declare the variable within your outer immediately-invoked function, which will enclose the variable and make it available to the two inner functions:
(function() {
var a;
function inner1 () {
a = 'string';
}
function inner2 () {
console.log(a);
}
inner1();
inner2(); // => logs 'string'
})()
console.log(a); // => logs undefined, because a is enclosed

In JavaScript, why does this global variable become undefined in my function? [duplicate]

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.

Using variables from other functions in Javascript [duplicate]

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

About the scope in javascript,why the following not output 21 but 22? [duplicate]

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.

Categories