0

Can we write the code like this in js to check the condition

input != (null || undefined) ? document.write(input) : document.write("Not Found");

I'm expecting to check whether the input is null or undefined

Code Maniac
  • 37,143
  • 5
  • 39
  • 60
  • `(input != null || input != undefined) ? ... : ... ` But since `document.write` is not an expression that returns a value, you can also use a normal `if/else` block, no need for the ternary. – Thilo Jul 06 '19 at 04:07
  • You need to put all condition inside a (conditions) ? ... : ... – Brajesh Singh Jul 06 '19 at 04:10
  • You could do `document.write(input ? input : "Not Found")` if null and undefined are the only possible [falsy](https://developer.mozilla.org/en-US/docs/Glossary/Falsy) values for `input`. But, this will write `Not Found` if input is an empty string or 0 (type number not "0") – adiga Jul 06 '19 at 04:13

1 Answers1

2

The way you're doing is not right,

input != (null || undefined) 

This will always be treated as

input !=  undefined

because || logical OR always returns first truthy value if there's any if not than returns the last value

You can do it either

input != null || input != undefined

or you can

[null,undefined].some(val => val != input)
Code Maniac
  • 37,143
  • 5
  • 39
  • 60