Syntax
- We start with the if keyword
- followed by open parenthesis
(
- then the condition. Which is for example
age >= 18
or could begrade === 10
depending on what you're checking in the if condition. - then followed by a closing parenthesis
)
- 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. - 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.
- 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))