JavaScript: Conditional Code
You can start building more complex JavaScript statements by using conditional code. The conditional code allows you to ask if a condition is met. If the condition is met, a certain code block is run. If the condition isn't met, a different code block is run. The answer to the condition must always be a yes or no answer.
Conditional Code Format
Here's the format of the conditional code:
The statement begins with if
followed by a condition in parentheses. After the condition, the code block is run. Code blocks are contained within curly braces { }
. The else
means "if the previous condition isn't met, do this instead," or more simply, "Otherwise."
Here's an example with some information:
Conditional Operators
In the condition statement, you can use a variety of operators to ask questions:
Equals Signs
Note that the equals operator is two equals signs ==
, not one. When you set the value of a variable, you use one equals sign =
. One equals sign means "assign as" whereas two equals signs means "equals".
There's also a triple equals sign ===
, which means "is strictly equal to." This is a more formal way of checking conditions. For example, if you have a variable that's a number, such as var a = 10
, and another variable that's a string, such as var a = "10"
, the two variables are different types of variables and therefore aren't "strictly" equal, even though they're the same.
Nested Conditions
You can nest multiple conditions. A nested condition looks like this:
Usually you don't nest more than a couple of condition levels, so that the code doesn't become too complicated.
And/Or Operators
You can create more advanced conditions by including &&
(and) or ||
(or) operators. Here's an example of the "and" operator:
Here's an example with "or" operator:
For clarity, you can surround the conditions in their own parentheses, like this:
Just remember that you always need both opening and closing parentheses. And just as with more simple conditional statements, the answer needs to be either yes or no.
Arithmetic Operators
You can use a variety of arithmetic operators:
Here's an example:
This is pretty straightforward: if a plus b is equal to 100. However, when you combine multiple arithmetic operators, JavaScript runs multiplication and division first, and then addition and subtraction. This prioritization is called "operator precedence." So if the operation is the following:
The result will be 26 rather than 48. You multiply 12 * 2 to get 24, and then add 2 to get 26.
To specify that the addition or subtraction runs first, you can surround it in parentheses:
Now the result will be 48.
The Remainder Operator
The remainder operator refers to what's left over when a number doesn't divide evenly into another number. Here's an example of the remainder operator in action:
The value for the remainder would be 2. This is because 4 goes into 2010 (the year) 502 times, with a remainder of 2.
Arithmetic Shorthand
Because arithmetic operators are used so frequently, there's a shorthand way of writing them. When you see +=
, it means to add the number that follows to the variable. Conversely, -=
means subtract the number from the variable, and so on.
The following are the long and short ways of writing the same operators:
Unary (++
) Operators
There's another shorthand for arithmetic operators: ++
is called a unary operator. When you add ++
with a variable, it means to increment the variable by 1. Similarly, if you add --
, it means decrease the variable by 1. Here are a few examples:
Ternary Operators
There's one more shorthand operator to mention. A ternary operator has three components and reduces what would otherwise occupy several lines of code into just one line. Rather than writing out:
You can compress this into a couple of lines in the following format:
After stating the condition, add a question mark ?
. After the question mark, write what should happen if the condition is true; then add a colon. On the right of the colon, write what should happen if the condition is false.
Here's how the ternary operator would be written using the previous example:
In other words, if a
is greater than b
, assign the highScore
variable as a
. If it's not, assign the highScore
variable as b
.
About Tom Johnson
I'm an API technical writer based in the Seattle area. On this blog, I write about topics related to technical writing and communication — such as software documentation, API documentation, AI, information architecture, content strategy, writing processes, plain language, tech comm careers, and more. Check out my API documentation course if you're looking for more info about documenting APIs. Or see my posts on AI and AI course section for more on the latest in AI and tech comm.
If you're a technical writer and want to keep on top of the latest trends in the tech comm, be sure to subscribe to email updates below. You can also learn more about me or contact me. Finally, note that the opinions I express on my blog are my own points of view, not that of my employer.