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

[JavaScript Tutorial] JavaScript comparison and logical operators

Dec 24, 2016 pm 03:08 PM

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


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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Python Operators: The Ultimate Guide from Newbie to Master Python Operators: The Ultimate Guide from Newbie to Master Mar 11, 2024 am 09:13 AM

Introduction to python operators Operators are special symbols or keywords used to perform operations between two or more operands. Python provides a variety of operators covering a wide range of uses, from basic mathematical operations to complex data manipulation. Mathematical operators Mathematical operators are used to perform common mathematical operations. They include: operator operation examples + addition a + b - subtraction a-b * multiplication a * b / division a / b % modulo operation (take the remainder) a % b ** power operation a ** b // integer division (discard the remainder) a//b Logical Operators Logical operators are used to concatenate Boolean values ​​and evaluate conditions. They include: operator operations examples and logical and aandbor logical or aorbnot logical not nota comparison operations

What are the logical operators in Python? What are the logical operators in Python? Oct 18, 2023 am 11:05 AM

What are the logical operators in Python? Logical operators in Python are used to logically compare expressions and return a Boolean value (True or False). There are three commonly used logical operators in Python: and, or and not. and operator and operator is used to check whether all operands are true (True). The and operator returns True only if all operands are true; otherwise it returns False. Here is a sample code: a=10b=

The Secret Garden of Operators: Discover Hidden Treasures in Python The Secret Garden of Operators: Discover Hidden Treasures in Python Mar 11, 2024 am 09:13 AM

The Secret Garden of Operators Python operators are symbols or keywords used to perform various operations. They enable developers to express complex logic concisely and clearly and improve code efficiency. Python provides a wide range of operator types, each with its specific purpose and usage. Logical Operators Logical operators are used to combine Boolean values ​​and perform logical operations. The main ones are: and: Returns the Boolean value True, if all operands are True, otherwise it returns False. or: Returns a Boolean value True if any operand is True, otherwise returns False. not: Negate the Boolean value, change True to False, and change False to True. Demo code: x=Truey

How to correctly use the logical OR operator in C language || How to correctly use the logical OR operator in C language || Mar 29, 2024 pm 12:45 PM

Title: How to correctly use the logical OR operator || in C language. In C language, the logical OR operator || is a commonly used logical operator, which is used to determine whether any of the conditions is true. Proper use of logical OR operators can help us write more concise and efficient code. The following will introduce in detail how to correctly use the logical OR operator || in C language and provide specific code examples. The basic syntax of the logical OR operator || is: expression 1 || expression 2. When either expression1 or expression2

Uncovering the Power of Python Operators: Writing Elegant and Efficient Code Uncovering the Power of Python Operators: Writing Elegant and Efficient Code Mar 11, 2024 am 09:28 AM

Python operators are a key component of the programming language, enabling developers to perform a wide range of operations, from simple arithmetic to complex bit manipulation. Mastering the syntax, semantics, and functionality of operators is essential to using Python effectively. Arithmetic Operators Arithmetic operators are used to perform basic arithmetic operations. They include addition (+), subtraction (-), multiplication (*), division (/), modulo (%), exponentiation (**), and floor division (//). The following example demonstrates the use of arithmetic operators: >>a=10>>b=5#Addition c=a+bprint(c)#Output: 15#Subtraction c=a-bprint(c)#Output: 5#Multiplication c=a*bprint(c)#output

An in-depth understanding of Python operators: a practical guide to bitwise operators, logical operators, and operator precedence An in-depth understanding of Python operators: a practical guide to bitwise operators, logical operators, and operator precedence Jan 20, 2024 am 09:51 AM

Application of Advanced Python Operators: Practical Guide to Shift Operators, Logical Operators, and Operator Priority Python is a high-level programming language widely used in various fields, and it is very important to master the use of operators. In addition to basic arithmetic operators, Python also provides many other types of operators, including bitwise operators, logical operators, etc. This article will delve into the application of these operators and provide specific code examples to help readers better understand and use them. 1. Bit shift operator bits

Secrets of Python Operators: Mastering the Cornerstone of Programming Secrets of Python Operators: Mastering the Cornerstone of Programming Mar 11, 2024 am 09:19 AM

Python operators are special symbols or words used to perform specific operations on values ​​or to combine values. They are the fundamental building blocks of programming languages ​​and are key to understanding and writing efficient code. Arithmetic Operators Arithmetic operators are used to perform basic mathematical operations such as addition, subtraction, multiplication, division, and remainder. The following are the most commonly used arithmetic operators: +Addition-Subtraction*Multiplication/Division%Remainder Example: x=10y=5print(x+y)#Output: 15print(x-y)#Output: 5print(x*y)#Output :50print(x/y)#Output: 2.0print(x%y)#Output: 0 Comparison Operator The comparison operator is used to compare two values ​​and return a Boolean value (True

Java regular expression logical operators Java regular expression logical operators Aug 30, 2023 am 09:45 AM

JavaRegularexpressionssupports3logicaloperatorstheyare−XY:XfollowedbyYX|Y:XorY(X):capturinggroup.XY:XfollowedbyYThissimplymatchestwosingleconsecutivecharacters.Example LiveDemoimportjava.util.Scanner;importjava.util.regex.Matcher;i

See all articles