Skip to main content

Syntax


  1. We start with the if keyword
  2. followed by open parenthesis (
  3. then the condition. Which is for example age >= 18 or could be grade === 10 depending on what you're checking in the if condition.
  4. then followed by a closing parenthesis )
  5. then followed by an open curly brace {. This denotes the start of the if block. Which determines what would happen if the condition inside the if was true.
  6. You could have whatever expression here. We haven't learned about expressions yet, but you could return here if you were in a function or change the value of a variable.
  7. finally, you should close the opening curly brace with a closing one: }

The final code would look like this:

if (age >= 18) {
//do something
return something;
}

// like this:
function canVote(age) {
if (age >=18) {
return true;
}
return false;
}

Returning some quick code to check positivity or negativity:

function isPositive(number) {
return number >= 0;
}

// Sample usage - do not modify
console.log(isPositive(5))
console.log(isPositive(-10))