DEV Community

Gayathri.R
Gayathri.R

Posted on

"JavaScript Comparison & Logical Operators — Made Simple for Beginners!"

JavaScript isn’t just about printing messages or changing colors on a webpage—it also makes decisions. How does it know whether a user is old enough to sign up? Or if a password is correct? That’s where comparison and logical operators come in.

comparison operator

These operators are used to compare two values and return true or false. 
Enter fullscreen mode Exit fullscreen mode

Types of Comparison Operators:

== Equal (compares values only)

=== Strict equal (compares values and types)

!= Not equal (values only)

!== Strict not equal (values or types not same)

Greater than

< Less than

= Greater than or equal to

<= Less than or equal to
double(==):
14 == 6(true)
"10" == 10 (true) --> Dynamic typing

(triple ===): Strictly checks the datatype too.. "10"===10 -->false 
Enter fullscreen mode Exit fullscreen mode

arithmetic operators

Arithmetic operators are symbols that help you do math with numbers — like adding, subtracting, multiplying, and dividing.

    • (Addition) This operator is used to add two values. Example: 5 + 3 gives 8. It simply adds numbers together.
    1. - (Subtraction) This operator subtracts the second number from the first. Example: 10 - 4 gives 6.
    2. * (Multiplication) This is used to multiply two numbers. Example: 4 * 2 gives 8.
    3. / (Division) This divides one number by another. Example: 8 / 2 gives 4.
    4. % (Modulus) This gives the remainder after dividing two numbers. Example: 10 % 3 gives 1 because 3 x 3 = 9 and 1 is left over.
    5. ++ (Increment) This operator increases the value by 1. Example: If a = 5, then a++ becomes 6.
    6. -- (Decrement) This operator decreases the value by 1. Example: If b = 5, then b-- becomes 4. Example no++ means no=no+1; no+=2//no=no+2;

    no=no*3
    no*=3

    no=n/3;
    no/=3;

    Logical operators

    Logical operators are used to check multiple conditions at the same time. They help your code make smart decisions.
    And
    Or
    Not

    Not operator and Not equal operator

    10!=10 --> false
    10!="10" --> false
    10!=="10" --> True.
    Example:

let loggedIn = false; if (!loggedIn) { console.log("Please log in"); } 
Enter fullscreen mode Exit fullscreen mode

##And

let no=10; (no++>10)&&(--no<15); console.log(no); Output: no=10 
Enter fullscreen mode Exit fullscreen mode

Conclusion

Understanding arithmetic, comparison, and logical operators is the first step to writing smart and powerful JavaScript code. These basic tools help you do math, compare values, and make decisions in your programs. Once you master them, you'll be ready to build real logic into your websites and apps.

Top comments (0)