Comparison operators are used in logical statements to determine equality or difference between variables or values. Operator Description:
-
Equal (==): Returns true if the operands are equal.
-
Not equal (!=): Returns true if the operands are not equal.
-
Strict equal (===): Returns true if the operands are equal and of the same type. See also Object.is and sameness in JS.
-
Strict not equal (!==): Returns true if the operands are of the same type but not equal, or are of different type.
-
Greater than (>): Returns true if the left operand is greater than the right operand.
-
Greater than or equal (>=): Returns true if the left operand is greater than or equal to the right operand.
-
Less than (<): Returns true if the left operand is less than the right operand.
-
Less than or equal (<=): Returns true if the left operand is less than or equal to the right operand.
Logical operators are typically used with Boolean (logical) values; when they are, they return a Boolean value. However, the && and || operators actually return the value of one of the specified operands. Logical operators:
- Logical AND (&&): expr1 && expr2 usage. Returns expr1 if it can be converted to false; otherwise, returns expr2. Thus, when used with Boolean values, && returns true if both operands are true; otherwise, returns false.
- Logical OR: expr1 or expr2 usage. Returns expr1 if it can be converted to true; otherwise, returns expr2. Thus, when used with Boolean values, or returns true if either operand is true; if both are false, returns false.
- Logical NOT (!): !expr usage. Returns false if its single operand that can be converted to true; otherwise, returns true.
Loops are used in JavaScript to perform repeated tasks based on a condition. Conditions typically return true or false when analysed. A loop will continue running until the defined condition returns false.
- Java provides three types of loop statements while loops, do-while loops, and for loops: • for - loops through a block of code a number of times • while - loops through a block of code while a specified condition is true