Home >Web Front-end >JS Tutorial >[JavaScript Tutorial] JavaScript comparison and logical operators

[JavaScript Tutorial] JavaScript comparison and logical operators

黄舟
黄舟Original
2016-12-24 15:08:261060browse

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

Example

== Equals x ==8 false Example»

x==5 true Example»

=== Absolutely equal (value and type are equal) x==="5" false Example»

x===5 true Example»

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

!== Definitely not equal (neither value nor type is equal) x!=="5" true Example»

x!==5 false Example»

> Greater than x>8 false Example»

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


How to use

You can use comparison operators in conditional statements to compare values ​​and then take action based on the results:

if (age<18) x="Too young";

You will learn more about conditional statements in the next section of this tutorial Knowledge.

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 1) is true

|| or (x==5 || y==5) is false

! not !(x==y) is true

Conditional Operator

JavaScript also contains functions based on certain conditions Conditional operator for assigning values ​​to variables.

Syntax

variablename=(condition)?value1:value2

Example

Example

If the value in the variable age is less than 18, assign the value "age is too young" to the variable voteable, otherwise assign the value "age has reached".

voteable=(age<18)?"年龄太小":"年龄已达到";

The above is the content of [JavaScript Tutorial] JavaScript comparison and logical operators. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn