JavaScript Operator: Not Operator, And Operator, OR Operator
Table of Contents
Control statements:
JavaScript Operator:- In a sequence logic structure, the statements in a program are executed one after the other in the order in which they are written. This is called sequential execution. In sequential execution, no statement is skipped or executed more than once.
Every programming language has features that allow the programmer to control the execution of programs. These statements are known as control statements. Using control statements, you can execute or ignore a set of statements. Similarly you can execute a set of statements repeatedly.
In JavaScript, example of control statements are: if, if-else, switch, while, do-while, and for statements. The control statements can be categorized as:
- Conditional Statements.
- Looping statements.
We can discuss the Looping statement in next article. In this article, we can discuss only Conditional Statements in very detail.
Amazon Purchase Links:
*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!
Conditional Statements:
The statements that are used to execute or ignore a statement or a set of statement of the program on the basis of some condition is known as conditional statements or decision-making statements or selection statements. A condition must be give with conditional statement. The computer tests the given condition, if it is true, the set of statements under the selection statement is executed otherwise it is ignored.
JavaScript provides the following conditional statement or selection statements/structures:
- If selection structure.
- If-else selection structure.
- Switch selection structure.
Before discussing the conditional statements, relational JavaScript Operators and expression and logical JavaScript Operators and expression are described. These JavaScript Operators and expressions are used for making test conditions in selection statement (also in looping statements).
Relational JavaScript Operators and Expression:
The relational JavaScript Operator is used to test a relation between two values or expressions and returns true or false as output. The relational JavaScript Operators are also known as comparison operators.
Relational or comparison JavaScript Operators are mostly used in conditional and looping statement to make test conditions. During execution of program/script, computer tests the condition and takes action depending on the result of test condition.
The relational JavaScript Operators with symbols, meaning and examples are given below. Suppose, values of variables A is 10, B is 5 and C is 10, then:
JavaScript Operator | Meaning | Description | Example |
== | Equal to | Returns true if both values are equal; otherwise return false. | A==B returns false |
=== | Exactly equal to | Returns true if both values are exactly equal with respect to value and type. | B===5 is true while
B===”5” is false |
!= | Not Equal to | Returns true if both values are different; otherwise returns false. | A!=B returns true |
> | Greater than | Returns true if first value is greater than second; otherwise returns false | A>B returns true |
< | Less than | Returns true if first value is less than second; otherwise returns false. | A<B returns false |
>= | Greater than or equal to | Returns true if first value is greater than or equal to second; otherwise returns false. | A>= C returns true |
<= | Less than or equal to | Returns true if first value is less than or equal to second; otherwise returns false | A<=C returns true |
The above mentioned JavaScript Operators can be used to compare all type of data such as numeric and string value. The values being compared may be constant values or variables.
Relational Expression:
An expression that contains two operands and one relational JavaScript Operator between these operands is called relational expression. the relational expression returns true (or 1) or false (or 0). The relational expression is used in conditional statement (or looping statement) as test condition. In JavaScript, true can be represented by any non-zero value while false can be represented by zero value.
For example, a>b is a relational expression, it returns true if the value of ‘a’ is greater than ‘b’, otherwise it returns false.
Suppose a=6, b=9 and c=6, then the relational expressions and their output values are shown in the following table:
Relational Expression | Output |
a > c | False |
a >= c | True |
a < b | True |
a <= c | True |
A >= c | True |
a < b | True |
a <= c | True |
a == b | False |
a === 6 | True |
a === ”6” | False |
A != c | False |
Logical JavaScript Operators & Expression:
In addition to relational JavaScript Operators, JavaScript also provides logical operators. These JavaScript Operators are used to form more complex and useful logical condition typically, the logical JavaScript Operators are used to combine more one relational expressions and the resulting expression is called compound expression. The logical Operators of JavaScript are given below.
AND (&&) JavaScript Operator:
It is used to combine two relational expressions and returns true if both expression are true. In this case, if first expression is false, the evaluation process is stopped at that point and second expression is not evaluated.
OR (||) JavaScript Operator:
It is used to combine two relational expressions and returns true of either of the expressions is true. In this case, if fires expression is false, the evaluation process will continue and second expression is evaluated.
NOT (!) JavaScript Operator:
It is used to reverse the result of the relational expression or compound expression.
Logical Expression:
An expression that is collection of relational expression (or simple operands) joined together by logical JavaScript Operators is called logical expression or compound expression. like relational expression, the logical expression also returns true (or 1) or false ( or 0). Some examples of logical expressions are:
- a && b
- c || d
- ! (a < b)
For example, if a=10, b=5 and c=10, the table below shows the output of the compound expressions as:
Compound Expression | Output |
(a > b ) && (a > c) | False |
(a>b) || (b=c) | True |
!(a>b) | false |
Examples:
If ‘total’ , ‘math’, ‘eng’ and ‘computer’ are four integer variables. The variable ‘total’ store the sum of the values of variable ‘math’, ‘eng’ and ‘computer’.
- To find out if the value in variable ‘total’ is greater than or equal to 700 and the value in variable ‘computer’ is greater than 90, the compound expression is written as:
Total >=700 && computer >90
- To find out if value of ‘total’ is greater than 600 or value in ‘eng’ is greater than 70, the compound expression is written as:
Total > 600 || eng > 70
- To find out if value in ‘computer’ variable is not greater than 33, the expression is:
!(computer > 33)
Conditional JavaScript Operator:
The conditional JavaScript Operator is denoted by ? and : signs. It is also used for making two-way decisions. The conditional JavaScript Operator is closely related to the simple ‘if-else’ structure. It can be use as an alternative to simple ‘if-else’ structure.
The general syntax of conditional JavaScript Operator is :
(condition) ? exp1 : exp2
Where
Condition:
Represents the condition. It may be a relational or logical expression. if it is true then computer will take action on “exp1 and “exp2” is ignored. It condition is false, the computer will take action on exp2 and exp1 is ignored.
Exp1, exp2:
Both these expressions may be arithmetic expression or constant values or variables. Other statements of JavaScript such as input and output statement can also be used in place of exp1 and exp2
It may be noted that the conditional operator takes three operands (i.e. condition, exp1 and exp2). It is a ternary operator.
for example, to test and print whether a give number ‘ n’ is odd or even, the statement using conditional JavaScript Operator is written as:
(n%2==0)?document.write(“even”):document.write(“odd number”)