Variable name in the global context



  • I noticed the odds:

    var name = 0;
    name = name || 4;
    console.log(name);
    console.log(typeof name);

    I don't understand why I'm out. 0 and stringif both numbers and 0 Like, false♪ The idea should come out. 4 and Number

    But if you write such a function:

    function new1() {
        var name = 0;
        name = name || 4;
        console.log(name); 
        console.log(typeof name); 
    }
    new1();

    That makes it right: 4 and Number



  • One of the invisible features. https://developer.mozilla.org/ru/docs/Web/API/Window/name

    In implementing this code in a global context, this characteristic is used instead of variables, as an attempt is made to add a single field to the window in an attempt to identify variables in a global context.

    Because there's already a characteristic in this object. name It's just called a grid and it's recorded.

    Then, as far as the number 0 is checked, the row 0, which is not equivalent to false.

    console.log(window.name)
    var name = 10;
    console.log(window.name, name, window.name === name);

    The proclamation of the variable by the operator can also be used for rounding. https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Statements/let in this case, there is no addition to a global facility

    let name = 0;
    name = name || 4;
    console.log(name);
    console.log(typeof name);


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2