Conditions in JavaScript - ProgrammerTech
Language:

Search

Conditions in JavaScript

  • Share this:
Conditions in JavaScript

Conditional statements in javascript

Conditions are the way a computer makes decisions, and they always have a part that contains an if statement. The if statement is the part responsible for telling the application what to do if the condition is true. The conditional sentence also contains a second part, which is the else clause, which is the part responsible for telling the application what to do if the condition is incorrect.

 

Operators used with conditions in JavaScript

  1. < greater than .
  2. > smaller than .
  3. =< greater or equal to.
  4. => Less or equal to.
  5. == The two values ​​are equal regardless of the data type means 1 equals "1".
  6. =! The two values ​​are not equal.
  7. === The two values ​​are equal and of the same data type means 1 does not equal "1".
  8. ==! The two values ​​are not equal considering the data type.

 

If conditional statement in javascript

The if statement specifies the path of the program under certain conditions, and it is the part responsible for telling the program what to do if the condition is met.

The general form of the if statement in JavaScript

if ( condition ) {
	/The command to be executed if the condition is met.
}

The condition evaluates to true,false. If the condition evaluates to true, the program will run, and if it evaluates to false, it will be the opposite and the program will not run.

Example 1

if ( 5 > 15 ) {
	alert ("Condition fulfilled");
}

The result in the browser will be empty because the condition is not met because the 5 is not greater than 15. If we reverse the sign in the condition

if ( 5 < 15 ) {
	alert ("Condition fulfilled");
}

Then the condition will be fulfilled, and when we execute the code in the browser, we will have a pop-up dialer that tells us that the condition has been met.

 

else conditional clause in javascript

The else clause is the part that tells the program what to do if the condition is false. As in the first example, instead of giving us an empty result or no result, it will give us a result if the condition is not satisfied.

General form of else clause in JavaScript

if ( condition ) {
	/The command to be executed if the condition is met
}
else{
	/The command to be executed if it is not met
}

The first part of any conditional sentence is the if statement, and it contains the condition and commands if this condition is satisfied. The second section is the else clause, which is what commands will be carried if the condition is not met.

Example 2

if ( 5 > 15 ) {
	alert ("Condition fulfilled");
}
else{
	alert("Condition not met");
}

When running this code, it will not give us an empty or no result because it is not validated, but it will print the statement that we set. The condition is not met.

Example 3

var points = 60;
if ( points >= 40 ) {
	alert ("successful");
}
else{
	alert("fail");
}

We created a variable for scores, which is points, and it had a score of 60. We created a conditional sentence and said it, if the score is greater or equal to 40, print the word Success. Otherwise, print a deposit. When we run the code, it will print the result for us as successful because the condition is true because the value of the variable points is already greater than 40 which is 60.

 

else if conditional clause in javascript

The else if clause is used if we want the program to check more than one conditional statement, we put else if instead of else.

Example 4

var points = 95;
if (points >= 90){
    alert("excellent");
} else if (points >= 80){
    alert("very good");
} else if (points >= 70){
    alert("good");
} else if (points >= 60){
    alert("Acceptable");
} else {
    alert("weak");
}

We created a scalar variable which is the point and assigned it the value 95 and we created a conditional sentence, and the conditions in this sentence are:

  • If the value of the variable points is greater than or equal to 90, print Excellent.
  • If the value of the variable points is greater than or equal to 80, print very fine.
  • If the value of points is greater than or equal to 70, print OK.
  • If the value of the variable points is greater than or equal to 60, print Acceptable.
  • And if it is otherwise, print weak, that is, the fraction limit is only 60, and less than it is weak.

It will print the result for us Excellent because the value of the variable is 95 is greater than the condition 90, you can change the value of the variable 95 and experiment with all the values.

 

switch clause in javascript

We use the switch clause if we want to test the value of a particular variable with the list of possibilities we set. And if this value is equal to any possibility, then only the orders that we put in this possibility will be executed.

The general form of the switch statement in javascript

switch (exprission) {
    case1 value:
        /statments
        break;
    case2 value:
        /statments
        break;
    case3 value:
        /statments
        break;
    default:
        /statments
        break;
}

exprission means the variable we want to test, case means the value, and the statements mean the commands we want to be executed. This means that if the value of the variable tested above is equal to any case at the bottom, only the commands of the case whose value is equal will be executed. The break was set to stop when its request is executed and not continue on the cases after it.

Example 5

var date = 1992;
switch (date) {
    case 1990:
        alert("date = 1990");
        break;
    case 1992:
        alert("date = 1992");
        break;
    case 1994:
        alert("date = 1994");
        break
    case 1996:
        alert("date str = 1996");
        break;
    default:
        alert("invailed");
        break;
}

We created a scalar variable for the date and its value is 1992, and we set several conditions for this date, and one of the possibilities will be executed. The possibilities are:

  • If the value of the variable is 1990, then implement the first possibility and carry the text date = 1990.
  • If the value of the variable is 1992, then implement the second possibility and carry the text date = 1992.
  • If the value of the variable is 1994, implement the third possibility and carry the text date = 1994.
  • If the value of the variable is 1996, implement the fourth possibility and carry the text date = 1996.
  • And if it is not all of the above, then print the word “invailed”.

When we run the code we will get the second result because it is achieved.

conditions javascript if javascript else javascript

Tags:
Muhammad Alush

Muhammad Alush

إسمي محمد تركي العلوش من سورية من ريف حلب الشرقي 1994, محب للتقنية و التعلم الذاتي, و جميع خبراتي تعلمتها ذاتياً, و اللغات البرمجية التي أتقنها هي html, css, javascript, php, c, python بالإضافة إلى حبي الكبير لمجال الشبكات.

موقعي programmer-tech.com