JavaScript Javascript and Hoisting javascript hoisting functions,javascript hoisting why,javascript hoisting let,javascript

In my code, the x value is undefined. If I remove if block, the x value is displayed as 77. I don't understand why if block is modifying the x value.

var x = 77;

function fn() {
  if (false) {
    var x = 55;
  }
  console.log(x);
}

fn();
Answer:1

var x = 77;

function fn() {
  if (false) {
   var x = 55;
  }
  console.log(x); // undefind
}

fn();
Answer:2

The x variable is redeclared and hoisted inside the function, but never set because if (false) will never be reached thus undefined. The outer x is known inside the function if you remove the inner declaration.

This can be solved by using const or let (ES6) instead of var. const and let is not hoisted and lives only inside of the brackets they are declared:

const x = 77;

function fn() {
  if (false) {
    const x = 55;
  }
  console.log(x);  // 77
}
fn()

Another solution is to just use two different variable names or remove the var inside the if statement depending on your needs...

Answer:3

Inside the scope of the function, the variable x is hoisted to the top as follows var x; If you print it, you get undefined. It is never assigned a variable as it is not going inside the if block

Answer:4

Simply remove the var inside the if-statement. That will fix the hoisting issue.

var x = 77;

function fn() {
    if(false) {
        x = 55;
    }
    console.log(x); // 77
}

fn();
Answer:5

You can also use let keyword.

let x = 77;

function fn() {
  if (false) {
    let x = 55;
  }
  console.log(x); // 77
}

fn()
Answer:6

I have a JavaScript validation on form, every time the form is submitted it will empty the boxes. I need to change it, when the form is filled up incomplete with empty boxes. it will not clear all the ...

I have a JavaScript validation on form, every time the form is submitted it will empty the boxes. I need to change it, when the form is filled up incomplete with empty boxes. it will not clear all the ...

  1. styling input validation
  2. custom field validation message

I got myself stuck in a pyramid of doom using Promises. I have the following: getA getB getC getC depends on getB (and getA) that depends on getA. So I must call them like this getA(param) ....

I got myself stuck in a pyramid of doom using Promises. I have the following: getA getB getC getC depends on getB (and getA) that depends on getA. So I must call them like this getA(param) ....

  1. callback hell with promises
  2. callback hell in javascript
  3. callback hell in node js
  4. callback hell in js
  5. callback hell in node js example
  6. node js callback hell
  7. callback hell in node
  8. callback hell in java
  9. callback hell in angular 2
  10. callback hell in angular
  11. callback hell in swift
  12. callback hell in angularjs
  13. callback hell in react
  14. callback hell in javascript stackoverflow
  15. callback hell in angular 6
  16. callback hell in es6
  17. callback hell to rxjs
  18. callback hell in
  19. callback hell in typescript
  20. callback hell in ios

I am trying to create a set of elements from CMS. I have reproduced the problem here with a set of rectangles that are being generated in Javascript. How can I dynamically add a "onmouseover" method ...

I am trying to create a set of elements from CMS. I have reproduced the problem here with a set of rectangles that are being generated in Javascript. How can I dynamically add a "onmouseover" method ...

I want to change the arrow style function (at onChange) in this react component into an ordinary function. I'm a beginner and for me it is helpful to see both versions side by side since I have a hard ...

I want to change the arrow style function (at onChange) in this react component into an ordinary function. I'm a beginner and for me it is helpful to see both versions side by side since I have a hard ...

  1. arrow function into normal function