javascript 2

Nailing the Basics

So how do we define a variable?


Simply type in


    var x = 5
    
And check it was defined by typing


    x
    
and you should see the value of x printed

How do we run tests on the variable?

Introducing the 'if' statement


    if(true){
        // run true code
    } else {
        // run false code
    }
    
Here is an example on our 'x' variable from before


    if(x > 5){
        alert('X was larger than 5');
    } else {
        alert('X was less than or equal to 5');
    }
    

What are the operators?

  • >  - Greater Than
  • <  - Less Than
  • >= - Greater Than or Equal To
  • <=   - Less Than or Equal To
  • ==   - Equal to
  • !=    - Not Equal to

More rare operators

  • !==  - Not Equal To (strict comparison)
  • === - Equal (strict comparison)
See here for more details on strict comparison

BOOLEAN LOGIC

Need to check two things?
without Boolean logic:


    if(x > 3){
        if(x < 6){
            alert('x was greater than 3 and less than 6');
        }
    }
with Boolean Logic


    if(x > 3 && x < 6){
        alert('x was greater than 3 and less than 6');
    }

BOOLEAN OPERATOR

  • && - AND (both sides must be true)
  • || -  OR (either of the sides must be true)
  • !  -  NOT (flips a statement between true and false)

Not Operator


            if(!(x > 4)){
                alert('x is not greater than four (x is less than or equal to 4)');
            }
            



That's a wrap

Questions?

Made with Slides.com