js的比較運(yùn)算符的理解

5.5. Relational Operators
This section describes the JavaScript relational operators. These are operators that test for a relationship (such as "less-than" or "property-of") between two values and return true
or false
depending on whether that relationship exists. As we'll see in Chapter 6, they are most commonly used in things like if
statements and while
loops, to control the flow of program execution.
5.5.1. Comparison Operators
The most commonly used types of relational operators are the comparison operators, which are used to determine the relative order of two values. The comparison operators are:
Less than (<
)

The <
operator evaluates to true
if its first operand is less than its second operand; otherwise it evaluates to false
.

Greater than (>
)

The >
operator evaluates to true
if its first operand is greater than its second operand; otherwise it evaluates to false
.

Less than or equal (<=
)

The <=
operator evaluates to true
if its first operand is less than or equal to its second operand; otherwise it evaluates to false
.

Greater than or equal (>=
)

The >=
operator evaluates to true
if its first operand is greater than or equal to its second operand; otherwise it evaluates to false
.

The operands of these comparison operators may be of any type. Comparison can be performed only on numbers and strings, however, so operands that are not numbers or strings are converted. Comparison and conversion occur as follows:
If both operands are numbers, or if both convert to numbers, they are compared numerically.

If both operands are strings or convert to strings, they are compared as strings.

If one operand is or converts to a string and one is or converts to a number, the operator attempts to convert the string to a number and perform a numerical comparison. If the string does not represent a number, it converts to NaN
, and the comparison is false
. (In JavaScript 1.1, the string-to-number conversion causes an error instead of yieldingNaN
.)

If an object can be converted to either a number or a string, JavaScript performs the numerical conversion. This means, for example, that Date objects are compared numerically, and it is meaningful to compare two dates to see whether one is earlier than the other.

If the operands of the comparison operators cannot both be successfully converted to numbers or to strings, these operators always return false
.

If either operand is or converts to NaN
, the comparison operator always yields false
.

Keep in mind that string comparison is done on a strict character-by-character basis, using the numerical value of each character from the Unicode encoding. Although in some cases the Unicode standard allows equivalent strings to be encoded using different sequences of characters, the JavaScript comparison operators do not detect these encoding differences; they assume that all strings are expressed in normalized form. Note in particular that string comparison is case-sensitive, and in the Unicode encoding (at least for the ASCII subset), all capital letters are "less than" all lowercase letters. This rule can cause confusing results if you do not expect it. For example, according to the <
operator, the string "Zoo" is less than the string "aardvark".
For a more robust string comparison algorithm, see the String.localeCompare( )
method, which also takes locale-specific definitions of "alphabetical order" into account. For case-insensitive comparisons, you must first convert the strings to all lowercase or all uppercase using String.toLowerCase( )
or String.toUpperCase
( ).
The <=
(less-than-or-equal) and >=
(greater-than-or-equal) operators do not rely on the equality or identity operators for determining whether two values are "equal." Instead, the less-than-or-equal operator is simply defined as "not greater than," and the greater-than-or-equal operator is defined as "not less than." The one exception is when either operand is (or converts to) NaN
, in which case all four comparison operators return false
.

5.5.2. The in Operator
The in
operator expects a lefthand operand that is or can be converted to a string. It expects a righthand operand that is an object (or array). It evaluates to true
if the lefthand value is the name of a property of the righthand object. For example:
var point = { x:1, y:1 }; // Define an objectvar has_x_coord = "x" in point; // Evaluates to truevar has_y_coord = "y" in point; // Evaluates to truevar has_z_coord = "z" in point; // Evaluates to false; not a 3-D pointvar ts = "toString" in point; // Inherited property; evaluates to true

5.5.3. The instanceof Operator
The instanceof
operator expects a lefthand operand that is an object and a righthand operand that is the name of a class of objects. The operator evaluates to true
if the lefthand object is an instance of the righthand class and evaluates to false
otherwise. We'll see in Chapter 8 that, in JavaScript, classes of objects are defined by the constructor function that is used to initialize them. Thus, the righthand operand of instanceof
should be the name of a constructor function. Note that all objects are instances of Object
. For example:
var d = new Date(); // Create a new object with the Date( ) constructord instanceof Date; // Evaluates to true; d was created with Date( )d instanceof Object; // Evaluates to true; all objects are instances of Objectd instanceof Number; // Evaluates to false; d is not a Number objectvar a = [1, 2, 3]; // Create an array with array literal syntaxa instanceof Array; // Evaluates to true; a is an arraya instanceof Object; // Evaluates to true; all arrays are objectsa instanceof RegExp; // Evaluates to false; arrays are not regular expressions

If the lefthand operand of instanceof
is not an object, or if the righthand operand is an object that is not a constructor function, instanceof
returns false. On the other hand, it returns a runtime error if the righthand operand is not an object at all.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市厅篓,隨后出現(xiàn)的幾起案子琼梆,更是在濱河造成了極大的恐慌,老刑警劉巖捌臊,帶你破解...
    沈念sama閱讀 212,718評(píng)論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件杨蛋,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)逞力,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,683評(píng)論 3 385
  • 文/潘曉璐 我一進(jìn)店門曙寡,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人寇荧,你說我怎么就攤上這事举庶。” “怎么了揩抡?”我有些...
    開封第一講書人閱讀 158,207評(píng)論 0 348
  • 文/不壞的土叔 我叫張陵户侥,是天一觀的道長。 經(jīng)常有香客問我峦嗤,道長蕊唐,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 56,755評(píng)論 1 284
  • 正文 為了忘掉前任烁设,我火速辦了婚禮替梨,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘装黑。我一直安慰自己副瀑,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,862評(píng)論 6 386
  • 文/花漫 我一把揭開白布恋谭。 她就那樣靜靜地躺著俗扇,像睡著了一般。 火紅的嫁衣襯著肌膚如雪箕别。 梳的紋絲不亂的頭發(fā)上铜幽,一...
    開封第一講書人閱讀 50,050評(píng)論 1 291
  • 那天,我揣著相機(jī)與錄音串稀,去河邊找鬼除抛。 笑死,一個(gè)胖子當(dāng)著我的面吹牛母截,可吹牛的內(nèi)容都是我干的到忽。 我是一名探鬼主播,決...
    沈念sama閱讀 39,136評(píng)論 3 410
  • 文/蒼蘭香墨 我猛地睜開眼清寇,長吁一口氣:“原來是場噩夢啊……” “哼喘漏!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起华烟,我...
    開封第一講書人閱讀 37,882評(píng)論 0 268
  • 序言:老撾萬榮一對情侶失蹤翩迈,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后盔夜,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體负饲,經(jīng)...
    沈念sama閱讀 44,330評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡堤魁,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,651評(píng)論 2 327
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了返十。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片妥泉。...
    茶點(diǎn)故事閱讀 38,789評(píng)論 1 341
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖洞坑,靈堂內(nèi)的尸體忽然破棺而出盲链,到底是詐尸還是另有隱情,我是刑警寧澤迟杂,帶...
    沈念sama閱讀 34,477評(píng)論 4 333
  • 正文 年R本政府宣布刽沾,位于F島的核電站,受9級(jí)特大地震影響逢慌,放射性物質(zhì)發(fā)生泄漏悠轩。R本人自食惡果不足惜间狂,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 40,135評(píng)論 3 317
  • 文/蒙蒙 一攻泼、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧鉴象,春花似錦忙菠、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,864評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至淆游,卻和暖如春傍睹,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背犹菱。 一陣腳步聲響...
    開封第一講書人閱讀 32,099評(píng)論 1 267
  • 我被黑心中介騙來泰國打工拾稳, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人腊脱。 一個(gè)月前我還...
    沈念sama閱讀 46,598評(píng)論 2 362
  • 正文 我出身青樓访得,卻偏偏與公主長得像,于是被迫代替她去往敵國和親陕凹。 傳聞我的和親對象是個(gè)殘疾皇子悍抑,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,697評(píng)論 2 351

推薦閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 9,442評(píng)論 0 23
  • 隨著互聯(lián)網(wǎng)時(shí)代成為我們生活當(dāng)中的一個(gè)新常態(tài),變革時(shí)時(shí)刻刻都在發(fā)生杜耙!作為一個(gè)食品行業(yè)的從業(yè)者搜骡,對于變革的感受越來越深...
    OwenZ閱讀 512評(píng)論 0 2
  • wps2016
    Feel君閱讀 64評(píng)論 0 0
  • 還有135天,我不知道自己前幾個(gè)月干了些啥佑女。感覺自己還沒有認(rèn)真準(zhǔn)備考研浆兰。在家一個(gè)多月磕仅,天天墮落了。原本打算明年考特...
    賞心悅事閱讀 214評(píng)論 0 0
  • 夜晚的風(fēng)簸呈,寒冷榕订,雨,瓢潑蜕便。 可是劫恒,阻擋不了“年輕人”愛玩的心。為什么“年輕人”帶引號(hào)轿腺,因?yàn)椋?并不是所有年齡小的人...
    了渺藍(lán)閱讀 420評(píng)論 2 0