Introduction to js comparison and logical operators

Source: Internet
Author: User

Comparison and logical operators are used to test true or false.

Comparison Operators
Comparison operators are used in logical statements to determine whether variables or values are equal.

Given x = 5, the following table explains the comparison operators:

Operator Description Example
= Equal X = 8 is false
=== Full (Value and type) X = 5 is true; x = "5" is false
! = Not equal X! = 8 is true
> Greater X> 8 is false.
< Less X <8 is true
> = Greater than or equal X> = 8 is false
<= Less than or equal X <= 8 is true
How to Use
You can use a comparison operator in a condition statement to compare values and then take actions based on the results:

If (age <18) document. write ("Too young ");
In the next section of this tutorial, you will learn more about conditional statements.

Logical operators
Logical operators are used to determine the logic between variables or values.

Given x = 6 and y = 3, the following table describes the logical operators:

Operator Description Example
&& And (X <10 & y> 1) is true
| Or (X = 5 | y = 5) false
! Not ! (X = y) is true
Conditional Operators
JavaScript also contains conditional operators that assign values to variables based on certain conditions.

Syntax
Variablename = (condition )? Value1: value2
Example
Greeting = (visitor = "PRES ")? "Dear President": "Dear ";
If the value in the variable visitor is "PRES", the value of "Dear President" is assigned to the variable greeting; otherwise, the value of "Dear" is assigned ".

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.