JavaScript undefined as a valid variable name in local scope

To work around accidental modification of undefined, you should not write this dangerous word in your code at all.

Since you only need read access to undefined, it is recommended to always use void 0 instead, which returns undefined.

var person = {
   method: function() {

       var undefined = 2

       if(void 0) {
          console.log("this should never be printed")
       } else {
          console.log("void 0 is always undefined, no matter what you do")
       }

   }
}

person.method()
Answer:1

Welcome to the wonderful world of JavaScript!

There is no way to prevent someone from doing this, but there is a way to ensure that undefined will mean undefined if you set up your functions as follows (not that you should really have to do this because it would be very bad practice for anyone to actually set up a variable named undefined). Essentially smaller scoped functions could hide a higher scoped undefined variable.

// This is just a way to get a variable called "undefined"
function wrapper(){
  var undefined = 10;
  console.log("wrapper says undefined is: " + undefined);

  // This function declared a parameter called "undefined",
  // but if the function gets called with no argument, then
  // the value of this, more local, argument will truly be
  // undefined. If arguments are needed, just add "undefined" 
  // to the end of the list.
  function foo(undefined){
    // Formal test to ensure that undefined is what we believe it to be:
    if(typeof undefined === "undefined"){
      console.log("foo says undefined is: " + undefined);
    }
  }
  
  // When foo is called with no arguments passed in, the parameter
  // "undefined" will take on a value of undefined within the scope
  // of that function.
  foo();
}

wrapper();
Answer:2

How i can update value in database using objection ? My SQL query works perfect. UPDATE "freePlace" SET number = number-1 WHERE date >= '2017-10-20' AND date <= '2017-10-30' AND "idObject" ...

How i can update value in database using objection ? My SQL query works perfect. UPDATE "freePlace" SET number = number-1 WHERE date >= '2017-10-20' AND date <= '2017-10-30' AND "idObject" ...

I have a ng-repeat div, and inside the div have select control. This code produces parse error: <select name="qty" ng-model="Qty"> <option ng-repeat="v in [1,2,3,4,5,6,7,8,9,10]" ...

I have a ng-repeat div, and inside the div have select control. This code produces parse error: <select name="qty" ng-model="Qty"> <option ng-repeat="v in [1,2,3,4,5,6,7,8,9,10]" ...

I'm trying to use a modal triggered from the ion-header-bar to overlay on top of a google map. When I click 'login' it slides in a black screen. I've heard of including bootstrap can cause the ...

I'm trying to use a modal triggered from the ion-header-bar to overlay on top of a google map. When I click 'login' it slides in a black screen. I've heard of including bootstrap can cause the ...

  1. ionic modal slide right
  2. ionic modal slide up
  3. ionic modal-slide-in
  4. ionic modal animation slide-in-down

How can I handle destructuring a nested object that might have an undefined internal object? For example. I have a xhr req that returns {person: user: {}} For example: const xhrResponse = {person: ...

How can I handle destructuring a nested object that might have an undefined internal object? For example. I have a xhr req that returns {person: user: {}} For example: const xhrResponse = {person: ...

  1. object destructuring nested
  2. destructuring nested object javascript
  3. destructuring nested object es6
  4. destructuring nested object js
  5. destructuring nested objects undefined
  6. destructure nested object react
  7. destructuring assignment nested object
  8. js object destructuring nested
  9. javascript object destructuring nested
  10. es6 object destructuring nested