Comparison operators in JavaScript>, <, >=, <= introduction _ javascript tips-js tutorial

Source: Internet
Author: User
This article mainly introduces the comparison operators & gt;, & lt;, & gt;, and & lt; in JavaScript. This article first describes the use rules of operators, then, an operation example is provided. For more information, see comparison operators (>,<>=, <=, and <=) with the = Operator) you can convert the object to string or number before comparison -- for number, compare the size of the value; for string, compare the order of Characters in the encoding table. Unlike the = Operator, = converts the Date object to a string first and then compares it, the comparison operator converts all objects including Date into numbers first and then compares them. The comparison rules are as follows:

1. If there is an object on both sides of the operator, convert it to number. If it cannot be converted to number, convert it to string.
2. After conversion, if both sides of the operator are strings, the string is compared; otherwise, the value is compared as long as number appears on one side.
3. If NaN appears on both sides of the operator, false is returned.
4.0 is equal to-0.

Lab


The Code is as follows:


// In comparison, Date object is converted to number
Var d = new Date ();
Var s1 = "Thu Mar 27 2008 14:57:11 GMT + 0800 (CST )";
Var s2 = "Thu Mar 27 2099 14:57:11 GMT + 0800 (CST )";
Var n1 = d. valueOf ()-1000;
Var N2. = d. valueOf () + 1000;
Console. log (d> s1); // false, d is converted to number, and that number is further converted to string. It is a string comparison here.
Console. log (d> s2); // false
Console. log (d> n1); // true
Console. log (d> n2); // false

Console. log ("11"> 3); // true

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.