Not operator
You can stick a NOT (!) in front of any boolean expression to invert its result. This opens the door to express the same logical statements in different ways.
Examples
Example: Typical Usage
Truth table for the boolean NOT operator.
console.log(!true);
console.log(!false);
Example: Take Your Temperature
Check (two different ways) for temperature in a good range or not.
// Check for temperature in a good range or not.
textLabel("tempLabelID", "What is your temperature?");
textInput("tempID", "");
button("buttonID", "Submit");
textLabel("tempMessageID1", "");
textLabel("tempMessageID2", "");
onEvent("buttonID", "click", function(event) {
setText("tempMessageID1","");
setText("tempMessageID2","");
var temp = getText("tempID");
if (!(temp >= 98 && temp <= 99.5) ) {
setText("tempMessageID1", "You may be sick.");
}
else {
setText("tempMessageID1", "Your temperature is fine.");
}
if (!(temp >= 98) || !(temp <= 99.5) ) {
setText("tempMessageID2", "You may be sick.");
}
else {
setText("tempMessageID2", "Your temperature is fine.");
}
});
Example: Working 9 to 5
Determines if it is currently working hours.
// Determines if it is currently working hours.
function IsWorkingHours() {
var now = new Date();
var hours = now.getHours();
var workHours = false;
if (hours >= 9 && hours < 17) {
workHours = true;
}
return workHours;
}
if (!IsWorkingHours()) {
console.log('take a break');
} else {
console.log('get to work');
}
Syntax
!expression
Parameters
Name | Type | Required? | Description |
---|---|---|---|
expression | boolean | The boolean expression to invert the result of. |
Returns
Tips
- Some complex decisions can sometimes be rewritten to use an ! operator. It is fine to choose whichever reads clearest.
Found a bug in the documentation? Let us know at support@code.org.