JavaScript Quirks Analysis

Source: Internet
Author: User

It's interesting to learn about some of the programming quirks of JavaScript, and it's worth summarizing:

1. Unknown variable name Create global variable

in our usual programming of JavaScript, some people are not very formal, when defining variables, the direct definition of "variable name = value" is not preceded by "Var", JavaScript automatically defines it as a global variable view

But in ECMAScript5 's rigorous mode get a warning:

For example: function f () {' Use strict '; foo = 123} f () Referenceerror:foo are not defined

2. Two null values of underfined and NULL

For "null" or "null reference", most programming languages have only one value. For example, in Java null , 2 values (undefined and null) are given in JavaScript

underfined:

<script type= "Text/javascript" >        var  s;        Console.log (s);     </script>

Results:

Similarly: If there is no value in the operation of the value parameter, a undefined is automatically assigned.

  <script type= "Text/javascript" >       functionreturn  x}       Console.log (x);   </script>

Running results in Firefox:

Running results in IE: Khan! Firefox at least to a undefined,ie directly on the judgment is undefined, this mechanism is too lazy ...

null: Is used by the developer to explicitly indicate that a value is missing

During the development process, we may need to pass this parameter without a value, and set the parameter equal to NULL for this scenario.

School check: Does a variable have a value?

 <script type= "Text/javascript" > //  var   X;  if   (x) {Console.log ( ' has value ' 
    
     ); 
     else   {Console.log ( ' no value ' 
   
    ); }  </script> 
   

If you want to know the variable x &NBSP; undefined &NBSP; null . Fortunately, the two values are false. Therefore, it is possible to test whether the two items are true at the same time using only one judgement, &NBSP; false , &NBSP; -0 , &NBSP; +0 , &NBSP; NaN   and "as null values. If this is not what you want, then you cannot use the school check method above. There are 2 ways to choose

Some people like to use "! =" for parameter checking

JavaScript Quirks Analysis

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.