[JavaScript tutorial] JavaScript comparison and logical operators

Source: Internet
Author: User
Comparison and logical operators are used to test true or false.

JavaScript comparison and logical operators

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.


Operator

Description

Comparison

Return Value

Instance

= X = 8 false instance»

X = 5 true instance»

=== Absolutely equal to (Value and type are equal) x === "5" false instance»

X = 5 true instance»

! = Not equal to x! = 8 true instances»

! = Definitely not equal to (values and types are not equal) x! = "5" true instance»

X! = 5 false instances»

> Greater than x> 8 false instances»

<Less than x <8 true instance»

> = Greater than or equal to x> = 8 false instances»

<= Less than or equal to x <= 8 true instance»


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) x="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) is 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

Instance

If the value of age is less than 18, the voteable variable is assigned "the age is too small"; otherwise, the value "The age has reached ".

Voteable = (age <18 )? "The age is too small": "The Age has reached ";

The above is the content of JavaScript comparison and logical operators in the [JavaScript tutorial]. For more information, see PHP Chinese website (www.php1.cn )!

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.