Home >Web Front-end >JS Tutorial >[JavaScript Tutorial] JavaScript comparison and logical operators
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: 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 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". 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)! if (age<18) x="Too young";
variablename=(condition)?value1:value2
voteable=(age<18)?"年龄太小":"年龄已达到";