Comparison operations include checking if one integer is equal to (==), greater than (>), less than (<), greater than or equal to (>=), or less than or equal to (<=) another integer.
In JavaScript you can use comparison operators to perform these operations with your variables.
Many times you use comparison operators in If statements to compare variables. (You will learn more about If statements in the next lesson.)
Here is an example of a comparison operator inside an if statement below.
let gameAge = 13
let playerAge = 14
if (playerAge >= gameAge) {
player.say("You can play")
}
Below, we have provided you with the start of a code. Fill in the code to create a variable using the comparison operators of equal to, greater than, and less than that compares the scores of player1 (score of 7) and player2 (score of 5). Have the player say "Player 1 wins!" if the conditional is true.
let =
let =
if () {
player.say()
}
let player1 = 7
let player2 = 5
if (player1 >= player2) {
player.say("Player 1 wins!")
}
Now run the code by clicking the green start button in the bottom right of the MakeCode screen.
You will know your code is correct when you see the text "Player 1 wins!" appear on your screen.
When your code works as expected move on to the next NPC to continue learning about variables.
If it does not work as expected, try to fix and test again.