O'Reilly logo

Real World XML by Steven Holzner

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Creating JavaScript if Statements

You use the if statement in JavaScript to test your data and to execute some code if the test is true. Here's the basic form of the if statement:

if (condition) {
    code
}

Here, condition is the test that you want to make, and code is the code you want to execute if the condition is true. One thing to note here is that you must enclose the code to execute in curly braces, { and }.

So what kind of conditions can you check, and how do you do so? To construct a condition to test, you use the comparison operators, such as < (less than), > (greater than), == (is equal to), <= (is less than or equal to), or >= (is greater than or equal to).

Here's an example. in this case, I'm making sure that the value in a variable ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required