How to put an image in a Javascript warning

if conditions in JavaScript

If the content of a variable is to be compared and, depending on the content, an action is then carried out, this can be programmed using if conditions. In the following example, a variable with the name x is set to 3 and then compared whether the content of this variable x corresponds to the number 6. This is an example: in reality, the content of the number is generated from a user input or a time and thus different program sequences will also "happen". So to simplify this described structure:

Only the variable is output but no message is given that the numbers are identical, since 3 is not 6. Here's a warning, as this is the second most popular bug in JavaScript (because it's quick and easy to make mistakes). If it looks like this:

A message now appears stating that the numbers are identical. And the next message we get is that the number is now 6. What happened. We no longer have a comparison in the if-condition, instead we assign the content 6 to the variable x. And this process always generates "true" - and as soon as the result of the condition is "true", the lines between the curly brackets are executed.

There is a good practice here - 3 equal signs. This controls both the content and the data type!

In addition, we now want to see a message on the screen if the numbers are not identical. To do this, we can extend the if condition with an else section. The structure is the same again: after that come curly braces that enclose the area that is carried out if that is the case.

if-condition to test unequal

If you want to test for unequal, the structure looks like this: