Javascript IF conditions

You have already seen the basic structure of a JavaScript IF statement but sometimes the question you want to ask is a bit more complicated.

Greater and and less than

Rather than checking if two things are equal you can also compare which is bigger:

if (weight<targetWeight) {            
        

If the first variable is less than the second variable the code runs. Alternatively:

if (weight<=targetWeight) {
        

Now the code runs if the first is less than or equal to the second. Greater than works the same way:

if (weight>=targetWeight) {
        

NOT

if (meat!='chicken') {
        

If the variable meat contains a value which is NOT equal to the string then the code runs.

Multiple checks

You can check two (or more) conditions in an IF statement:

if (meat=='chicken' && side=='chips') {                
        

The && is saying that the first AND the second condition must be true. You can also say OR:

if (meat=='chicken' || side=='chips') {                
        

If either of the conditions is true the code will be run.

submit to reddit Delicious Tweet