Questions tagged [hoisting]
Hoisting means that local variables inside the scope of a function are treated as if they are declared at the start of the function's evaluation regardless of where they actually are declared.
439
questions
0
votes
1
answer
33
views
Kotlin Composable ViewModel Hoisting - Context
I'm transitioning from iOS SWIFT to Android Kotlin & Jetpack, and struggling a little with @composables, I'm not using activity/fragments. I'm creating separate ViewModel classes to hoist state &...
0
votes
0
answers
30
views
Do packages hoisted still install?
In regard to hoisting, I have this workspace structure:
root
-apps
-myApp
package.json
-packages
-myPackage
package.json
I have "react-native-reanimated": "~2....
1
vote
2
answers
37
views
Global variable declaration returns undefined, any reason for this
When I log input to the console, it returns undefined but when put inside a the anonymous function, it logs to the console as expected
I had tried the below code, and expected that the inputValue to ...
0
votes
0
answers
43
views
SetInterval() not updating innerHTML of an element
i made a setInterval function that is supposed to update a paragraph every second (technically it is supposed to also change a global variable in the final code, but i figured the problem stands ...
0
votes
1
answer
25
views
React Component hook variable will not update
I have a React component that gets data from a parent page/component. I use this data object (jobData) to populate a hook variable value when the component (a modal) is fired. The jobData looks like ...
0
votes
0
answers
15
views
If object's property cannot be hoisted, then why does "var app =exports = module.exports = {};" work at the beginning of module
I am a JS newbie poking around Express.js source code trying to hone in on my understanding of finer JS details. I am looking at the main express.js file defining an application here: https://github....
1
vote
1
answer
37
views
Javascript in Node.js shows bizarre behaviour involving functions and block scopes
Can anyone make sense of the difference in behaviour between the two snippets described below?
Snippet #1
{
function f() {return 1}
f = function() {return 2}
function f() {return 3}
}
...
1
vote
0
answers
18
views
Is there anyway to understand this? Can function parameters be shadowed by variables declared inside the function? [duplicate]
In JavaScript The output of the below code snippet is [2,1,2]
function func(x, f = () => x) {
var y = x;
x = 2;
return [x, y, f()];
};
console.log(func(1));
In JavaScript output of ...
0
votes
1
answer
59
views
Mutablestateof - State hoisting
What to do to manage one state in two functions? (openDialog)
How to transfer openDialog state from ProductDialog() to AppBar()?
I know I can make a separate class per state but I don't want to.
@...
0
votes
1
answer
21
views
How can I enable hoisting when using static variables and fields? [duplicate]
I am learning about static methods and fields to use in my app. I have written the following codes to test what I have learned:
class ClassWithStatic {
static childclass = new SubClass("Hello");...
-1
votes
1
answer
37
views
Push to array and convert to string
I need to pass a value as a string and not an array from inside an event listener to a global variable.
Currently when the page loads, I check for a cookie inside the event listener and pass that ...
0
votes
1
answer
33
views
Different behaviors between the same var name and function name VS two same function name [duplicate]
Recently, I received this interview question, I know it's weird to declare var name and function name the same, but the code runs without errors, and I don't know how to explain this behavior, hence ...
0
votes
0
answers
15
views
Why the answer "Undefined and sushi" and not "grapes and shushi"? [duplicate]
var favoriteFood = "grapes";
var foodThoughts = function() {
console.log(`Original favorite food: ${favoriteFood}`);
var favoriteFood = "sushi";
console.log(`New favorite food: ${favoriteFood}`...
0
votes
1
answer
115
views
A Javascript quiz about hoisting
var d = 1;
(function(){
d = '2'
console.log(typeof d)
function d() {
}
})()
console.log(typeof d)
Could you explain why the second log prints out "number"?
var d = 1;
(function(){...
0
votes
1
answer
40
views
why JavaScript report an error when I code like this?
If i code like
function foo() {
console.log(123);
}(6)
execute this code snippet, console.log nothing
but
function foo() {
console.log(123);
}()
the code snippet will produce SyntaxError
(6) ...
0
votes
2
answers
31
views
Javascript accessing variable declared later from a function [duplicate]
Here, lastname const variable is declared later for understanding purpose. it is not accessed from the top in hoistedName obvisously with a hoisting concept. But how can it be read from a function ...
0
votes
1
answer
45
views
How to prove that variable hoisting occurs with let declaration? [duplicate]
Executing an undeclared variable x gives error: Uncaught ReferenceError: x is not defined
Accessing a variable before declaration with let gives similar error: Uncaught ReferenceError: y is not ...
-1
votes
1
answer
804
views
Why am I getting "Cannot access 'server' before initialization" error in NodeJS?
I am getting the dreaded Cannot access 'server' before initialization error in code that is identical to code that's running in production.
The only things that have changed are my OS version (macOS ...
1
vote
1
answer
269
views
How to Hoist Mutiple States in compose?
I have been trying to figure out how to hoist multiple states in 1 single composable/ viewModel but without any luck.
Whenever I type in one of the fields, it types in all fields at the same time. ...
3
votes
2
answers
1k
views
Const is not defined in global scope but defined in local?
Why const is not defined in global scope but defined in local?
screenshoot from devTools
{
console.log(b);
const b = 2;
}
VM366:2 Uncaught ReferenceError: Cannot access 'b' before ...
0
votes
1
answer
105
views
How to call POST route functions to fetch API data in order (JS)?
When I access(for lack of a better term) the '/postUserInput' POST route, it either does not run getGeoNamesData() until the end, or it doesn't wait for the API fetch to finish. The data I get from ...
0
votes
0
answers
43
views
const and let fake hoisting in modules
I've found interesting behaviour while having let and const on module level.
func.mjs
export const func = () => {
return num1 + num2;
}
const num1 = 5;
const num2 = 6;
main.mjs
import {func} ...
0
votes
0
answers
26
views
Why hoisting in this situation isn't allowed? [duplicate]
I find an example in w3school like this:
carName = "Saab";
let carName = "Volvo";
So after compile it would be like this:
let carName;
carName = "Saab";
carName = "...
0
votes
2
answers
89
views
JavaScript hoisting for function expression and function declaration
I am learning hoisting in JavaScript and practicing some examples.
In an example I am confused of it's output.
Here is the example code:
var f = function(){
console.log("Expression");
}
...
1
vote
0
answers
512
views
How to figure out why package is not hoisted in yarn3 monorepo?
I'm using yarn 3 monorepo and for some reason, react-dom package is not hoisted properly:
The same version of react-dom is duplicated across packanges.
qnm react-dom
react-dom 18.1.0 ↰ 18 hours ago
├──...
1
vote
0
answers
117
views
How does function within addEventListener gets hoisted?
I found a weird behavior that I couldn't understand even reading MDN doc about hoisting.
It says that functions and variables gets moved to the top of the scope except variable initialization. So how ...
0
votes
0
answers
16
views
Functions and hoisting related question in Javascript [duplicate]
const fun = () => console.log(variable);
let variable;
variable = 'some string';
fun();
Hello, guys I,m interested how JS works in this situation. In the function declaration I'm logging ...
1
vote
1
answer
310
views
Why is a global variable not being updated inside a function that is NOT redeclaring it as a local variable? [duplicate]
Before anyone asks if I reviewed other questions, I did find this one (link below paragraph) and updated one of my variables because it was redeclared as a local variable. It should now be hoisted as ...
3
votes
1
answer
38
views
YDNJS: scope and closures hoisting wrong example [duplicate]
I am reading YDNJS: scope and closures, And in chapter 4 which talks about hoisting it says that
Function declarations that appear inside of normal blocks typically hoist to the
enclosing scope, ...
0
votes
1
answer
24
views
Trying to toggle different "screens" by using divs in css, javascript, using display: none or block. Everything works except for last toggle
I thought I understood how to do this because all my toggles work, except for the last one. I can't figure out why!! It's written in the same way as other ones before it. The one that doesn't work is ...
-1
votes
1
answer
1k
views
Hoisting in react components. How does it work?
I want to understand how hoisting works with respect to react component. Please check https://codesandbox.io/s/react-hello-world-forked-mzbyn6?file=/src/index.js
I have included Test, Test2 and const ...
0
votes
0
answers
46
views
What the heck is going on in Javascript scopes and hoisting? [duplicate]
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 ...
1
vote
4
answers
167
views
function hoisting - why is my function not defined properly?
function goo(){
console.log(foo)
console.log(foo())
{
function foo() {return 'im foo'}
}
}
goo()
I'm trying to see where the foo function definition is hoisted ...
0
votes
1
answer
212
views
Heroku Hobby Plan total dyno hours
In heroku hobby plan, there are 7$/dyno/month.
I know that in free plan there is 550 free dynos(without credit card) per month.
But I can't find out how many dyno hours I can enjoy in heroku hobby ...
0
votes
0
answers
23
views
What is the definition of "scope" when we are talking about function hoisting in JavaScript [duplicate]
I am learning JavaScript Function Hoisting, which says "the declaration of functions are moved to the top of their scope". Then I read the definition of "scope" from this doc, it ...
2
votes
2
answers
62
views
Hoisting vs Scope Chain [duplicate]
How come, in the first block of code, I get "Hello", but in the second block of code, I get undefined, then "bye"?
For the first block of code, I get that it is going up the scope ...
5
votes
1
answer
71
views
Confusion about Javascript Hoisting [duplicate]
I thought I grasped the concept of hoisting, but the code below got me confused. How does it return 1? Will the second example() function be hoisted above the first one?
function example() {
...
0
votes
0
answers
453
views
What is difference between let , const and var in case of variable shadowing /re declaration in different scope? [duplicate]
Please check below 2 code snippets the difference is redeclaring as const/let and var in foo function.
for var, it executes but for const and let it throughs error.
Please tell me why it behaves ...
1
vote
1
answer
926
views
Can you have a function in an inline powershell script in a Azure pipeline YAML file?
I want to be able to simplify some inline powerscript in an AzureCLI task using a function, similar to the following:
- task: AzureCLI@2
displayName: "My Task"
inputs:
...
0
votes
1
answer
74
views
How to solve Javascript Cycle reference?
Fisrt Code
HTML
<div>
<select class="a">
<option value="1">aaa</option>
<option value="2">bbb</option>
</...
1
vote
1
answer
164
views
Should I use script tag and style tag in ejs files?
I'm using ejs as my template engine with node.js, and I'm using ejs as different components for my pages, e.g. homepage might be composed of header.ejs + sidebar.ejs + main.ejs, each representing a ...
6
votes
2
answers
1k
views
Yarn nohoist without using workspaces
One of my projects suddenly failed to compile on a Windows laptop, where the exact same code was working on a Mac. I've read about hoisting and adding nohoist, which seemed to fix the problem for ...
0
votes
0
answers
64
views
is hoisting not followed by react js Proptypes?
Version 1: Below code works fine as it is showing me Proptypes warning in console.
const rootElement = document.getElementById('root')
function SayHello({ firstName, lastName }) {
...
0
votes
0
answers
42
views
hoisting is not cause print, it throws an error, why?
the next 3 lines throws an error:
variable=3;
console.log(variable);
let variable;
but I thought that the hoisting means that the 3 lines will be:
let variable;
variable=3;
console.log(variable);
I ...
0
votes
2
answers
141
views
Does Var variables get hoisted out of the IIFE? [duplicate]
I have a block of code and I want to exactly know the running order and why the result like this:
var variable = 10;
(()=>{
variable_3 = 35;
console.log(variable_3);
var ...
0
votes
0
answers
106
views
How do I run code before hoisted require statements?
I want to generate a file at the beginning of my program. This must happen before the module is loaded, as the module will try to load that file.
Here is my code:
require('dotenv').config();
const fs =...
0
votes
1
answer
27
views
How function declaration is affecting hoisting? [duplicate]
There below two codes print different outputs, despite the inner function not being called.
var a = 1;
function foo(){
a= 2;
}
foo();
console.log(a); // 2
But if I add a function with the same ...
0
votes
2
answers
65
views
JavaScript Hoisting - Hoisted code after Memory Creation phase
I was reading Kyle Simpson book: https://github.com/getify/You-Dont-Know-JS/blob/1st-ed/scope%20%26%20closures/ch4.md#functions-first.
But I don't fully understand this line "Notice that var foo ...
1
vote
2
answers
655
views
scope of variables and functions inside if block in javascript
Variables defined inside if block, are accessible outside the if block also
console.log(name) // undefined
if (true) {
var name = "harry"
console.log(name) // harry
}
console.log(name) // ...