Types: Simple JavaScript Code

Topics

Javascript 

   variables
       types can be: string number boolean 
       type is determined by context
   
   strings
   
   numbers
      input
      aritmetic operators, PEMDAS
      
   boolean
      true false
      used in conditions (x > y)

   types assigned by context     
      implicit 
         js tries to convert and the rules are messy
      explicit 
         Number() String() Boolean()
     

<!DOCTYPE html>
<!-- FILE: types.html -->
<!-- wyatt -->
<html>
<head> 
    <title>grammar: how to write js code</title>
    <meta charset="UTF-8" />

    <script>
            ** SEE JS BELOW **
    </script>            

    <style>    
            ** SEE CSS BELOW **       
    </style> 
    
</head>
<body>
   

JS Grammar

The double horizontal rule below ends the html in this file. Everything below that line is created "on the fly" by javascript...



</body> </html>

// wyatt
// assign values and observe
function messWithTypes()
{
   var  myNum;
   // create html output including text and variable values
   document.write('

The value of " var myNum; " is: ' + String(myNum) + ' and the type is ' + typeof(myNum) + '

'); myNum = 87.22; document.write('

The value of " myNum = 87.22; " is: ' + String(myNum) + ' and the type is ' + typeof(myNum) + '

'); myNum = 5; document.write('

The value of " myNum = 5; " is: ' + String(myNum) + ' and the type is ' + typeof(myNum) + '

'); myNum += 4; document.write('

The value of " mynum += 4; " is: ' + String(myNum) + ' and the type is ' + typeof(myNum) + '

'); myNum++; document.write('

The value of " mynum++; " is: ' + String(myNum) + ' and the type is ' + typeof(myNum) + '

'); myNum = myNum / 0; document.write('

The value of " mynum = myNum/0; " is: ' + String(myNum) + ' and the type is ' + typeof(myNum) + '

'); myNum = 0 / 0; document.write('

The value of " mynum = 0/0; " is: ' + String(myNum) + ' and the type is ' + typeof(myNum) + '

'); myNum = 7 + 2 * 3; document.write('

The value of " mynum = 7 + 2 * 3; " is: ' + String(myNum) + ' and the type is ' + typeof(myNum) + '

'); myNum = "7" + 2 * 3; document.write('

The value of " mynum = "7" + 2 * 3; " is: ' + String(myNum) + ' and the type is ' + typeof(myNum) + '

'); myNum = "cat"; document.write('

The value of " mynum = "cat"; " is: ' + String(myNum) + ' and the type is ' + typeof(myNum) + '

'); myNum = "cat" / "dog"; document.write('

The value of " mynum = "cat"/"dog"; " is: ' + String(myNum) + ' and the type is ' + typeof(myNum) + '

'); myNum = "cat" + "dog"; document.write('

The value of " mynum = "cat" + "dog"; " is: ' + String(myNum) + ' and the type is ' + typeof(myNum) + '

'); myNum = true; document.write('

The value of " mynum = true; " is: ' + String(myNum) + ' and the type is ' + typeof(myNum) + '

'); }

/*
wyatt
*/
   /* css rule */
   body /* tag selector */
   {
     background-color:white; /* property:value */
   }
   .sample /* class selector */
   {
     background-color:#ddd;  /* property:value */
     color:red;   /* property:value */
     padding:20px;
   }