Javascript comparison logic - let a = 1 . a === (3 || 1 ) false, why?

logical operators javascript
javascript string comparison
how to compare two strings in javascript if condition
ternary operator javascript
not operator
javascript comparison operators
logical or

I've tried to use this comparison statement and was almost sure that's gonna work, but it's not. Could you explain why ?

let a = 1 
a === ( 1 || 3 ) // true 
a === ( 3 || 1 ) // false 

Thanks for responses :)

When you use OR comparison if any one of the leftmost expression is true the entire expression results to true. Thus,

( 1 || 3 ) will select 1 as 1 is a defined value on the leftmost side and when you do a === ( 1 || 3 ) it will be true as a === 1 is true, given that a = 1.

let a = 1;
let rightCondition = ( 1 || 3 );
//this will give 1
console.log(a === rightCondition);

JavaScript Comparison and Logical Operators, Comparison and Logical operators are used to test for true or false . "5", true, Try it ». === equal value and equal type, x === 5, true, Try it ». x === "5", false, Try​  The last result states that "null is greater than or equal to zero", so in one of the comparisons above it must be true, but they are both false. The reason is that an equality check == and comparisons > < >= <= work differently. Comparisons convert null to a number, treating it as 0. That’s why (3) null >= 0 is true and (1) null > 0 is false.

I think the comparison you're looking for is actually:

let a = 1 
a === 1 || a === 3  // true

Comparison operators, A strict comparison (e.g., === ) is only true if the operands are of the same type and 1 != 2 // true 1 != '1' // false 1 != "1" // false 1 != true // false 0 != false // false 3 === 3 // true 3 === '3' // false var object1 = {'key': 'value'}, object2 = {'key': Otherwise, use the standard equality operators, which allow you to  That shows the first message with 1. The alert returns undefined, so OR goes on to the second operand searching for a truthy value. The second operand 2 is truthy, so the execution is halted, 2 is returned and then shown by the outer alert. There will be no 3, because the evaluation does not reach alert(3).

Integers like 1 and 3 (but not 0) are truthy in JavaScript. So, the first of your comparisons returns 1 because 1 is truthy and the leftmost operand and the second one returns 3 because 3 is also truthy and the leftmost operand. The comparison stops immediately when a truthy value has been found (starting at the left). So, in your second comparison, the program does not even look at the 1.

Comparison and Logical Operators in Javascript, Comparison and Logical Operators are two of the main features of most if not al. They coupled with conditional statements form one of the basic building and less than or equal to < <= //Logical Operators //And && //Or || //Not ! let x = 3 let y = 4 x == 3 //true x === “3” //false x!=5 //true x!== DISCUSS (1). Double equals is the appropriate way to compare strings in Javascript, it is returning false then there may be whitespace to the left and or right of one string. Put a .trim() on the end of the strings and my comparison should started working:

An Introduction to JavaScript Logical Operators By Examples, In this tutorial, you will learn how to use JavaScript logical operators including the logical AND operator ( && ) and the logical OR operator ( || ). you to compare variables and do something based on the result of that comparison. let b = false; let result = b && (1 / 0); console.log(result); 3) The Logical OR operator ( || ). The equality operator converts the operands if they are not of the same type, then applies strict comparison. If both operands are objects, then JavaScript compares internal references which are equal when operands refer to the same object in memory. 1 == 1 // true '1' == 1 // true 1 == '1' // true 0 == false // true 0 == null // false var

Review: Logic and if Statements (article), If we changed x to -1, we wouldn't see the message show up at all, since x will evaluate to true or false , because we have many comparison operators. with the equality operator ( === ), because they both use equal signs, but they if ((​myAge >= 0 && myAge < 3) || myAge > 90) { println('You\'re not quite in your peak​. JavaScript variables are loosely/dynamically typed and the language doesn’t care how a value is declared or changed. 2017.08.22: This article has been updated to reflect the current state of the

Comparison and Logical Operators, Use this article as a reference sheet for JavaScript comparison and logical operators. Comparison operators — operators that compare values and return true or false . Let's make sure there aren't any gaps in your knowledge. we can assert whether two values or expressions are equal with === , or, whether one value  Welcome to this weeks JavaScript Quickie — 3 Minute JavaScript Lessons. Increment & Decrement. The increment and decrement operators in JavaScript will add one (+1) or subtract one (-1), respectively, to their operand, and then return a value. The term operand is used extensively in this article. An operand is the quantity on which an

  • Have you looked up what using the || operator resolves to? It might be informative.
  • See Operator Precedence.
  • @mrCod3r you can tick mark the answer if it was useful for you. Glad to help :)
  • yes, at first I thought the result will be the same