Home > Web Front-end > JS Tutorial > body text

[JavaScript Tutorial] JavaScript comparison and logical operators

黄舟
Release: 2016-12-24 15:08:26
Original
950 people have browsed it

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»

< Less than x<8 true Example»

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

<=  Less than or equal to  x<=8  true  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";
Copy after login

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 < 10 && y > 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
Copy after login

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)?"年龄太小":"年龄已达到";
Copy after login

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)!


source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!