Global Pollution and Reference error 2.1

2nd Day                                                                                            See the source image 

   Hey Guys welcome once again in 2nd day of my blog writing.

  Q. Guys is it true?. We are declare variable in JavaScript with datatype like C, C++  etc.

  A. No, Because JavaScript variables are loosely-typed. Which means it doesn't require a datatype            to be declared. For example,

         var x;    // This is called variable declaration.                                var x=10; // This is called variable initialization      

  • Note: we can write variable like x=10;, but it may create a problem which is called global-pollution, Because its behave like global variable and anybody can modify its value. 
  • Lets see the reference error before go for global pollution.
  • Whenever we declare the variable locally and accessing globally then that moment  it is show the reference error.
  •  For Example of Reference error 
          Input:
  •       Output:
            
  •  For Example of Global pollution
  • In this Example ,we have not used var keyword due to this reason it is behave like global variable. 
  • Input:
          output:
                  

                                                              Thank you....

No comments:

Post a Comment