Questions tagged [logical-or]

An operator whose result will be true if one or more operands are true

125 questions
397
votes
8 answers

When should I use ?? (nullish coalescing) vs || (logical OR)?

Related to Is there a "null coalescing" operator in JavaScript? - JavaScript now has a ?? operator which I see in use more frequently. Previously most JavaScript code used ||. let userAge = null // These values will be the same. let age1 = userAge…
mikemaccana
  • 110,530
  • 99
  • 389
  • 494
212
votes
6 answers

"querySelectorAll()" with multiple conditions in JavaScript

Is it possible to make a search by querySelectorAll() using multiple unrelated conditions? If yes, how? And, how to specify whether those are AND or OR criteria? For example: How to find all forms, ps and legends with a single querySelectorAll()…
Alan Coromano
  • 24,958
  • 53
  • 135
  • 205
42
votes
2 answers

How to use "OR" using Django's model filter system?

It seems that Django's object model filter method automatically uses the AND SQL keyword. For example: >>> Publisher.objects.filter(name__contains="press", country__contains="U.S.A") will automatically translate into something like: SELECT ...…
NotSimon
  • 1,795
  • 1
  • 21
  • 31
25
votes
2 answers

Why does pandas "None | True" return False when Python "None or True" returns True?

In pure Python, None or True returns True. However with pandas when I'm doing a | between two Series containing None values, results are not as I expected: >>> df.to_dict() {'buybox': {0: None}, 'buybox_y': {0: True}} >>> df buybox buybox_y 0 …
politinsa
  • 3,480
  • 1
  • 11
  • 36
25
votes
4 answers

TypeScript's type 'string | string[]' is not assignable to type 'string', what is the 'string | string[]' type? How to cast them to string?

When I do TypeScript: let token = req.headers['x-access-token'] || req.headers['authorization'] as string; I have the following error: Argument of type 'string | string[]' is not assignable to parameter of type 'string' Does anyone know what…
user504909
  • 9,119
  • 12
  • 60
  • 109
23
votes
6 answers

Why is "||" the symbol for or?

I know that || represents the logical operation "or", but I'm curious if anyone knows the history of choosing that symbol. Was it just because it happened to be an unused symbol on the keyboard?
Erty Seidohl
  • 4,487
  • 3
  • 33
  • 45
21
votes
2 answers

How is the nullish coalescing operator (??) different from the logical OR operator (||) in ECMAScript?

ES2020 introduced the nullish coalescing operator (??) which returns the right operand if the left operand is null or undefined. This functionality is similar to the logical OR operator (||). For example, the below expressions return the same…
francis
  • 3,852
  • 1
  • 28
  • 30
12
votes
1 answer

Does logical or ignores second statement if first is true

In Java (Eclipse), when having a statement such as if (true || false), it will end up true but the question is will the compiler evaluate the second statement if the first is true? This is of an importance to me because I have an operation I need to…
Sharon Dorot
  • 542
  • 1
  • 4
  • 15
8
votes
5 answers

Operator precedence and evaluation order

I can't understand output of this program: #include using namespace std; int main() { int x = 1 , y = 1, z = 1; cout << ( ++x || ++y && ++z ) << endl; //outputs 1; cout << x << " " << y << " " << z ; //x = 2 , y = 1 , z = 1; …
shiva
  • 2,535
  • 2
  • 18
  • 32
7
votes
6 answers

Logical OR in JavaScript

I ran into a case where I have run both functions in a JavaScript or expression: function first(){ console.log("First function"); return true; }; function second(){ console.log("Second function"); return…
Philip
  • 160
  • 1
  • 8
5
votes
2 answers

Precedence: Logical or vs. Ternary operator

Consider the following: (EDIT: I've amended the function slightly to remove the use or braces with the ternary operator) function someFunction(start,end,step){ var start = start || 1, end = end || 100, boolEndBigger = (start < end); …
Pineda
  • 7,435
  • 3
  • 30
  • 45
5
votes
3 answers

Bitwise and/or with ternary operator

Look at this tiny snippet. y
plhn
  • 5,017
  • 4
  • 47
  • 47
5
votes
3 answers

Shell Script: Assignment-Or Operator (a= b || c)

As the title describes, what's the proper way to do an or-assignment (eg a= b || c) in shell scripting, specifically csh vs bash? I cannot test this, so perhaps the example above works. I thought this was pretty common in scripting languages, but…
vol7ron
  • 40,809
  • 21
  • 119
  • 172
4
votes
3 answers

Non-function do while loop in C

I'm new to programming and was hoping I could get some help. I'm attempting to create a 'do while' loop in C as part of my CS50 problem sets. I'm attempting to use two different conditions under 'while', but for some reason they are not being…
Disierd
  • 53
  • 4
3
votes
2 answers

How to logical OR multiple variables in Powershell

I thought this was simple. And I'm sure it is. But, I can't seem to crack it. I have 3 functions that return a true or false value. In a later if evaluation I am trying to logical or the 3 results together. Something like this: if (Fnc1 -or Fnc2 -or…
Appleoddity
  • 647
  • 1
  • 6
  • 21
1
2 3
8 9