Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

229
Views
How do I combine boolean with if else statement?

I am dealing with the question of:

Create a Java Script control structure using if..else statement. Declare one variable and use Boolean condition to determine whether the condition is true or false. If the first condition evaluates to true, then the program will run the first block of code. Otherwise, it will run the else block. Show your output using the given condition.

{

if weather is equal to thunderstorm;

 then display “Stay at home”;

else

 then display “Happy Journey”;

}

Is it possible to show the output in html? How can I fix my code to show the output? It is not working currently.

This is my code:

<body>
  <p id="demo"></p>
  <script>
    boolean thunderstorm = true;
    if (thunderstorm) {
      document.getElementById("demo").innerHTML = "Stay at home";
    } else {
      document.getElementById("demo").innerHTML = "Happy Journey";
    }
  </script>
</body>

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Welcome to SO :)

If you check your developer tools and look at the console, you should see an error like "message": "Uncaught SyntaxError: Unexpected identifier",. In this case that is coming from your usage of "boolean". In JavaScript we don't have to declare variable types like that, instead you would use const, let or var to declare a variable, like on my example below.

const thunderstorm = true;
if (thunderstorm){
    document.getElementById("demo").innerHTML ="Stay at home";
} else {
    document.getElementById("demo").innerHTML ="Happy Journey";
}
<p id="demo"></p>

With JavaScript the type of a variable is not as strict as in some other languages. To quote MDN:

JavaScript is a loosely typed and dynamic language. Variables in JavaScript are not directly associated with any particular value type, and any variable can be assigned (and re-assigned) values of all types

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!