Executing multiple codes if else js. Conditional Operators in Javascript - IF-ELSE Construct - Conditions in Javascript - Basics


Getting started with JavaScript conditional statements. Here we will look at the If-Else construct. Translated into Russian, this condition is read as If-Then.

But before we start talking about conditions in JavaScript, we can consider how and where they occur in real life.

For example, if it is clear in the evening, we will go to the park.

if this car costs less than $ 1000, then I will buy it, etc.

Thus, as you have probably already understood the condition "If" and the consequence "That" are found all the time in our life. That is, our behavior in various situations mainly depends on some conditions.

The same is true for programming languages. They have special constructions that allow you to set certain conditions and perform actions if the specified conditions are met or not met.

Let's try to implement some simple example of using conditional statements, or rather the If-Else construct in JavaScript.

First, let's take a look at how the If statement in JavaScript works.

To do this, we will first give an example below, and then we will analyze it.

My family and I go to the Park in the evening

What to look for in the example above?

First, on the equal signs == and assignment = in JavaScript. They should be distinguished: that is, first we create a variable and assign a value to it. Then, in the If condition, we talk about equality.

Secondly, when it is said about the fulfillment or non-fulfillment of the condition enclosed in curly braces (), then it should be understood that the JavaScript language accepts a condition either as Truth, or as False. That is, if the condition is True, as in our case, then the action enclosed in curly braces () is performed.

If the condition is False, as in the example below, then the condition enclosed in curly braces () will not be met.

This is how the conditional If statement works: if the condition is True - the action is performed, if False - it will not be executed. It's simple.

Now let's talk about how the If-Else construct works in JavaScript. Else translates to "Otherwise".

Let's go back to real life. In most cases, if any condition is met, then we do one thing. If it is not fulfilled - "Otherwise", then we do something else.

Let's continue working with the examples given earlier.

If it's clear in the evening, we'll go to the park, otherwise (if it is cloudy) we will stay at home and watch TV.

Or if this car costs less than $ 1000, then I will buy it, otherwise (if it costs more) I will go on a trip with this money.

JavaScript also has such an option - to provide an alternative ( do something else) if the condition is not met. In JavaScript, we can create similar conditions using the If-Else construct. Let's take an example.

We stay at home - watch TV

Let's analyze the given example.

So, if the condition is True, then the action following the If statement, enclosed in curly braces (), is executed.

If the condition is False, then the action following the Else operator is executed, also enclosed in curly braces ().

We've seen how the simple but common If-Else construct works in JavaScript. And here, for the future, it should be said that no matter how difficult the condition may be, the first thing that matters is whether it is True or False.

To consolidate the passed material " Conditional Statements in Javascript - IF-ELSE Construct»Consider another example.

Only now we use the If-Else condition when working with numbers.

Count is less than or equal to 10

Here, as in the previous examples, everything is simple. In this case, the count variable is equal to 10, that is, the condition is TRUE and a corresponding message is displayed on the screen.

In the example below, the condition is not met: the count variable is greater than 10, that is, the condition is FALSE, which means that the message following the Else operator is displayed on the screen.

The count variable is greater than 10

So, here we have looked at the simplest examples of using conditional operators in JavaScript. This is how it is - not at all difficult, the If-Else construction works.

Take the array of friends from the previous topic: var friends = ["Alexey", "Vyacheslav", "Gregory", "Nastya", "Pavel"];

Write a condition that checks : if the number of elements in the array is greater than or equal to 3, then a message is displayed stating that this is a large array with at least 3 elements. Otherwise, display a message stating that this is a small array with less than 3 elements.

Before writing a test condition, you need to remember and know how to count the number of elements in an Array. The length property will help us with this.

The same task could be done a little differently, using a shorter version of the code. The variable count, which stores the number of array elements, does not need to be created. See example below ...