!
(निषेध ऑपरेटर) के रूप में "नहीं", ||
(बूलियन-या ऑपरेटर) के रूप में "या" और &&
के रूप में "और" (बूलियन और ऑपरेटर) की Think। Operators और Operator Precedence देखें।
इस प्रकार
:
if(!(a || b)) {
// means neither a nor b
}
हालांकि
, De Morgan's Law का उपयोग कर, यह के रूप में लिखा जा सकता है:
if(!a && !b) {
// is not a and is not b
}
a
और ऊपर b
किसी भी अभिव्यक्ति हो सकती है (जैसे test == 'B'
या जो कुछ भी यह करने की जरूरत है) ।
एक बार फिर, अगर test == 'A'
और test == 'B'
, अभिव्यक्ति कर रहे हैं, 1 प्रपत्र के विस्तार पर ध्यान दें:
// if(!(a || b))
if(!((test == 'A') || (test == 'B')))
// or more simply, removing the inner parenthesis as
// || and && have a lower precedence than comparison and negation operators
if(!(test == 'A' || test == 'B'))
// and using DeMorgan's, we can turn this into
// this is the same as substituting into if(!a && !b)
if(!(test == 'A') && !(test == 'B'))
// and this can be simplified as !(x == y) is the same as (x != y)
if(test != 'A' && test != 'B')
स्रोत
2011-05-24 19:35:56
क्या ऐसा करने के लिए कोई छोटा रास्ता है (छद्म कोड): 'if (test === ('ए' || 'बी')) '(मैंने लॉजिकल सादगी के लिए'! 'हटा दिया है, मैं हूं अवधारणा के बारे में अधिक उत्सुक) –
'if (x == 2 | 3)' जैसे छोटे संस्करण के लिए अच्छा होगा। –