JavaScript testing conditional logic

 JavaScript testing conditional logic

In JavaScript we use if and else statements to test the conditional logic, to perform a specific action when a certain condition is met.

We use these for decision making or branching.


We include the conditions we need to test under parentheses ( ). These are Boolean in nature, thus yield true or false. If the condition is true, it executes a certain portion of code included in the curly braces { } else it wont. Let's look at an example.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>If else statements</title>
</head>
<body>
    <p id="para1"></p>

<script>
var speedOfCar = 60;
if(speedOfCar == 50){
  document.getElementById("para1").innerHTML = "Speed of car: " + speedOfCar;
else{
  document.getElementById("para1").innerHTML = "You are speeding!";
}
</script>
</body>
</html>




Assignment:
Change the value of speedOfCar and look at the variations.
Try to use other operators such as greater than > or less than < to simulate the results.


Post a Comment

0 Comments

React