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