Latest web development tutorials
×

JavaScript Tutorial

JavaScript Tutorial JavaScript Introduction JavaScript usage JavaScript Output JavaScript grammar JavaScript Statement JavaScript Notes JavaScript variable JavaScript type of data JavaScript Object JavaScript function JavaScript Scope JavaScript event JavaScript The string JavaScript Operator JavaScript Comparison JavaScript If...Else JavaScript switch JavaScript for JavaScript while JavaScript Break & Continue JavaScript typeof JavaScript Type conversion JavaScript Regular Expressions JavaScript error JavaScript debugging JavaScript Variable promotion JavaScript Strict mode JavaScript Use errors JavaScript Form validation JavaScript Keep the keyword JavaScript JSON JavaScript void JavaScript Code specification

JS function

JavaScript Function definition JavaScript Function parameter JavaScript Function call JavaScript Closure

JS HTML DOM

DOM Introduction DOM HTML DOM CSS DOM event DOM EventListener DOM element

JS Advanced Tutorial

JavaScript Object JavaScript Number JavaScript String JavaScript Date JavaScript Array JavaScript Boolean JavaScript Math JavaScript RegExp Object

JS Browser BOM

JavaScript Window JavaScript Window Screen JavaScript Window Location JavaScript Window History JavaScript Navigator JavaScript Pop-ups JavaScript Timing events JavaScript Cookies

JS Library

JavaScript Library JavaScript test jQuery JavaScript test Prototype

JS Examples

JavaScript Examples JavaScript Object instance JavaScript Browser object instance JavaScript HTML DOM Examples JavaScript to sum up

JS Reference Manual

JavaScript Object HTML DOM Object

JavaScript comparison and logical operators

Comparison and logical operators for testing true or false.


Comparison

In comparison logic statements used to measure variables or values ​​are equal.

x = 5, the table below explains the comparison operators:

Operators description Compare return value Examples
== equal x == 8 false Examples >>
x == 5 true Examples >>
=== Absolutely equal to (value and type are equal) x === "5" false Examples >>
x === 5 true Examples >>
! = not equal to x! = 8 true Examples >>
! == Absolutely not equal to (or type a value not equal) x! == "5" true Examples >>
x! == 5 false Examples >>
> more than the x> 8 false Examples >>
< Less than x <8 true Examples >>
> = greater than or equal to x> = 8 false Examples >>
<= less than or equal to x <= 8 true Examples >>


how to use

You can use comparison operators in conditional statements to compare values, and then to 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.


Logical Operators

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

Given x = 6 and y = 3, the table below explains the logical operators:

运算符 描述 例子
&& and (x < 10 && y > 1) 为 true
|| or (x==5 || y==5) 为 false
! not !(x==y) 为 true


Conditional operator

JavaScript also contains certain conditions based on the variable conditions of the assignment operator.

grammar

variablename =( condition )? value1 : value2

example

Examples

If the variable age is less than 18, then assigned to the variable voteable "too young", otherwise assign "Age has been reached."

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