JavaScript variable Declaration

Source: Internet
Author: User

JavaScript variable Declaration

You can use double quotation marks to declare strings in JavaScript, or you can use single quotation marks to declare strings. Primarily for convenience and HTML integration, avoid the hassle of escaping characters.

JavaScript is a dynamic type, so

var i=10;i= ' test ' is legal.

var m,n,x,y=10; it's legal.

Determine if a variable is available

In JavaScript, when a variable is evaluated, the first thing to do is to determine if the variable is not undefined or null.

There are two ways of judging, but there are differences, and the second one is recommended.

The first type:

<script type= "Text/javascript" >        //var n;        // alert (n+100);//The result is Nan, not a number.         // So before the calculation, it is possible to determine whether the variable is available        // The following method can be judged, but if you do not declare x, you can not use this way, will be error         if(n) {            alert (' can be used ');        } Else {            alert (' cannot be used ')        ;    } </script>

The second type:

<script type= "Text/javascript" >        // Use the following method, even if no x is declared, you can judge and conclude that the result is not available        //  var x=10;        // Note: Undefined does not enclose the single quotation mark, can only judge the case of the Declaration of X, the first kind of error when judging the case of not declaring X        if (typeof(x)! = ' undefined ' &&x!=null) {            alert (' can be used ');        } Else {            alert (' not available ');        }     </script>

JavaScript variable Declaration

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.