JavaScript学习-if语句

一、IF语句

例:

       <% -- JavaScript学习 -- %>
    
< script  type ="text/javascript"  language ="JavaScript" >
        
// if语句
         function  FunShowIf()
        {
            
var  a = 1 ;
            
var  b = a ++ ;
            
if (a > b)
            {
                window.alert(a
+ ' > ' + b);   
            }
            
else
            {
                window.alert(a
+ ' < ' + b);   
            }                      
        } 
     
</ script >

            
<% -- 测试使用的按钮 -- %>
            
< input  id ="Button1"  type ="button"  runat ="server"  value ="测试"  onclick ="FunShowIf();"   />

  

   二:变量为空

  如果定义了变量但没有赋值,此时X类型为Undefined。如果要检查X是否定义,则用X==null()或typeof(x)=='undefined'(类型为未定义)。例:   

     < script  type ="text/javascript"  language ="JavaScript" >
        
function  FunIfNull()
        {
            
var  x;
            
if (x == null )
            {
                alert(
" X为空! " );
            }
        }
        
function  FunIfUndefine()
        {        
            
var  x;
            
if ( typeof (x) == ' undefined ' )
            {
                alert(
" X为空! " );
            }
        }
        
function  FunIfSign()
        {        
            
var  x;
        
         x
= ()
            
if ( typeof (x) == ' undefined ' )
            {
                alert(
" X为空! " );
            }
        }
     
</ script >

            
<% -- 测试使用的按钮 -- %>
            
< input  id ="Button1"  type ="button"  runat ="server"  value ="测试"  onclick ="FunIfNull();"   />
            
< input  id ="Button2"  type ="button"  runat ="server"  value ="测试"  onclick ="FunIfUndefine();"   />

三:? : 语句

例:

         < script  type ="text/javascript"  language ="JavaScript" >
        
function  FunIfSign()
        {        
            
var  x;
            
var  a = 0 ;b = 3 ;            
            x
= (a > b) ? " a>b " : " a<b " ;
            alert(x);
        }
     
</ script >
            
<% -- 测试使用的按钮 -- %>
            
< input  id ="Button1"  type ="button"  runat ="server"  value ="测试"  onclick ="FunIfSign();"   />

    结果:a<b

你可能感兴趣的:(JavaScript)