Introduction to JS comparison and logical operators _ basics

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 a variable or value is equal.

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

operator Description Example
== Equals X==8 to False
=== Congruent (value and type) X===5 is true;x=== "5" to False
!= Not equal to X!=8 is True
> Greater than X>8 to False
< Less than X<8 is True
>= Greater than or equal to X>=8 to False
<= Less than or equal to X<=8 is True
How to use
You can compare values by using comparison operators in conditional statements, and then act on the results:

if (age<18) document.write ("Too Young");
You will learn more about conditional statements in the next section of this tutorial.

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

Given X=6 and y=3, the following table explains the logical operators:

operator Description Example
&& and (x < && y > 1) is True
|| Or (x==5 | | y==5) to FALSE
! Not ! (x==y) is true
Conditional operator
JavaScript also contains conditional operators that assign values to variables based on certain criteria.

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

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.