Javascript program using logical operators
Comparison and Logical operators are used to test for Show
Comparison OperatorsComparison operators are used in logical statements to determine equality or difference between variables or values. Given that How Can it be UsedComparison operators can be used in conditional statements to compare values and take action depending on the result: if (age < 18) text = "Too young to buy alcohol"; You will learn more about the use of conditional statements in the next chapter of this tutorial. Logical OperatorsLogical operators are used to determine the logic between variables or values. Given that
Conditional (Ternary) OperatorJavaScript also contains a conditional operator that assigns a value to a variable based on some condition. Syntaxvariablename = (condition) ? value1:value2 ExampleIf the variable age is a value below 18, the value of the variable voteable will be "Too young", otherwise the value of voteable will be "Old enough". Comparing Different TypesComparing data of different types may give unexpected results. When comparing a string with a number, JavaScript will convert the string to a number when doing the comparison. An empty string converts to 0. A non-numeric string converts to
When comparing two strings, "2" will be greater than "12", because (alphabetically) 1 is less than 2. To secure a proper result, variables should be converted to the proper type before comparison: age = Number(age); Try it Yourself » The Nullish Coalescing Operator (??)The Example let name = null; Try it Yourself » The nullish operator is supported in all browsers since March 2020:
There are four logical operators in JavaScript: Although they are called “logical”, they can be applied to values of any type, not only boolean. Their result can also be of any type. Let’s see the details. || (OR)The “OR” operator is represented with two vertical line symbols: In classical programming, the logical OR is meant to manipulate boolean values only. If any of its arguments are In JavaScript, the operator is a little bit trickier and more powerful. But first, let’s see what happens with boolean values. There are four possible logical combinations:
As we can see, the result is always If an operand is not a boolean, it’s converted to a boolean for the evaluation. For instance, the number
Most of the time, OR For example:
We can pass more conditions:
OR "||" finds the first truthy valueThe logic described above is somewhat classical. Now, let’s bring in the “extra” features of JavaScript. The extended algorithm works as follows. Given multiple OR’ed values:
The OR
A value is returned in its original form, without the conversion. In other words, a chain of OR For instance:
This leads to some interesting usage compared to a “pure, classical, boolean-only OR”.
&& (AND)The AND operator is represented with two ampersands In classical programming, AND returns
An example with
Just as with OR, any value is allowed as an operand of AND:
AND “&&” finds the first falsy valueGiven multiple AND’ed values:
The AND
In other words, AND returns the first falsy value or the last value if none were found. The rules above are similar to OR. The difference is that AND returns the first falsy value while OR returns the first truthy one. Examples:
We can also pass several values in a row. See how the first falsy one is returned:
When all values are truthy, the last value is returned:
Precedence of AND The precedence of AND So the code Don’t replace Sometimes, people use the AND For instance:
The action in the right part of So we basically have an analogue for:
Although, the variant with ! (NOT)The boolean
NOT operator is represented with an exclamation sign The syntax is pretty simple: The operator accepts a single argument and does the following:
For instance:
A double NOT
That is, the first NOT converts the value to boolean and returns the inverse, and the second NOT inverses it again. In the end, we have a plain value-to-boolean conversion. There’s a little more verbose way to do the same thing – a built-in
The precedence of NOT How do we declare JavaScript 3 logical operators?Logic operators are used to find the logic between variables in JavaScript. There are three logical operators in JavaScript: || (OR), && (AND), !
What is == and === in JavaScript?== in JavaScript is used for comparing two variables, but it ignores the datatype of variable. === is used for comparing two variables, but this operator also checks datatype and compares two values. Checks the equality of two operands without considering their type. Compares equality of two operands with their types.
What are the examples of logical operators?Logical Operators. What is && operator in JavaScript?Logical AND ( && ) evaluates operands from left to right, returning immediately with the value of the first falsy operand it encounters; if all values are truthy, the value of the last operand is returned.
|