spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / programming / javascript / diaries / 3

[previous] [next]

Senior Consultant/Information Security - permanent position (TX)
Next Step Systems
US-TX-Irving

Justtechjobs.com Post A Job | Post A Resume
Developer News
Mandrake Linux Founder Back, Virtually
Amazon: We're a Technology Company
Sun Expands MySQL With Closed Source

The JavaScript Diaries: Part 3

Comparison Operators

Operator Name Description
== Equal Returns true if both values are equal
!= Not Equal Returns true if both values are not equal
=== Strict Equals Returns true if both values are equal and are the same data type (Added in JavaScript 1.5)
!== Strict Not Equal Returns true if both values are not equal or not the same data type (Added in JavaScript 1.5)
> Greater Than Returns true if the value on the left side of the operator is greater than the value on the right side
>= Greater Than or Equal Returns true if the value on the left side of the operator is greater than or equal to the value on the right side
< Less Than Returns true if the value on the left side of the operator is less than the value on the right side
<= Less Than or Equal Returns true if the value on the left side of the operator is less than or equal to the value on the right side

These operators are used for making comparisons which are true or false. These are pretty self-explanatory so I won't go into too much detail here. Notice that the single equal operator ("=") is not shown here. That is because it is an assignment operator. It's used for assigning a value to a variable or other type of container. i.e. var myNum = 35.

Comparison operators can be used for both numbers and strings. A value in a variable (number or string data type) can also be compared. The JavaScript interpreter will retrieve the value of the variable and then compare it with the other value.

Below are some comparisons. I've used several to give you a broader understanding of these comparison operators. Look at them very carefully and they should make sense.

  • 25 == 25 // ("25 is equal to 25") true
  • 25 == "25" // ("25 is equal to 25, after it has been converted from a string to a number" [The JavaScript interpreter will convert the string "25" to a number, 25.] ) true

  • 25 != 25 // ("25 is not equal to 25") false
  • 25 != 34 // ("25 is not equal to 34") true

  • 25 === 25 // ("25 is strictly equal to 25") true
  • 25 === "25" // ("the number 25 is strictly equal to the string '25'" [In this case, the data type of the values is taken into account and is not converted.] ) false

  • 25 !== "25" // ("the number 25 is strictly not equal to the string '25'") true

  • 25 > 24 // ("25 is greater than 24") true

  • 25 >= 25 // ("25 is greater than or equal to 25") true
  • 25 >= 15 // ("25 is greater than or equal to 15") true

  • 25 < 37 // ("25 is less than 37") true

  • 25 <= 25 // ("25 is less than or equal to 25") true
  • 25 <= 15 // ("25 is less than or equal to 15") false
  • When comparing two strings, JavaScript converts each character to its ASCII value, beginning on the left and continuing to the right. Using the ASCII equivalent, a lower case letter is greater in value than an upper case one. For instance, in ASCII code "A"=65 and "a"=97. So, "A" > "a" would be false while "a" > "A" would be true.

    Assignment Operators

    Operator Name Description
    = Assign Assigns the value on the right side of the operator to the variable on the left
    += Add and Assign Adds the value on the right side of the operator to the variable on the left, and then assigns the new value to the variable on the left
    -= Subtract and Assign Subtracts the value on the right side of the operator from the variable on the left, and then assigns the new value to the variable on the left
    *= Multiply and Assign Multiplies the value on the right side of the operator by the variable on the left, and then assigns the new value to the variable on the left
    ⁄= Divide and Assign Divides the variable on the left side of the operator by the value on the right side, and then assigns the new value to the variable on the left
    %= Modulus and Assign Divides the variable on the left side of the operator by the value on the right side, and then assigns the remainder to the variable on the left

    These operators are used for assigning values to variables, elements, and properties. Many of these operators are useful when utilized within loops, as some of them perform simple calculations on the variables. They are pretty much self-explanatory. They will come into play more when we begin studying loops. Examples of their use are shown below.

    • Assign (=): x = y
      (We've already used this in assigning a value to a variable, i.e. var myNum = 15)
    • Add and Assign (+=): x += y means the same as x = x + y
    • Subtract and Assign (-=): x -= y means the same as x = x - y
    • Multiply and Assign (*=): x *= y means the same as x = x * y
    • Divide and Assign (/=): x /= y means the same as x = x / y
    • Modulus and Assign (%=): x %= y means the same as x = x % y


    home / programming / javascript / diaries / 3

    [previous] [next]

    internet.comearthweb.comDevx.commediabistro.comGraphics.com

    Search:

    Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

    Jupitermedia Corporate Info

    Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
    Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

    Whitepapers and eBooks

    Symantec Whitepaper: Converging System and Data Protection for Complete Disaster Recovery
    Intel Whitepaper: Comparing Two- and Four-Socket Platforms for Server Virtualization
    IBM Solutions Brief: Go Green With IBM System xTM And Intel
    HP eBook: Simplifying SQL Server Management
    IBM Contest: Are You the Next Superstar? Join the "Search for the XML Superstar" Contest to Find Out
    Intel PDF: Quad-Core Impacts More Than the Data Center
    Intel PDF: Virtualization Delivers Data Center Efficiency
    Go Parallel Article: PDC 2008 in Review
    Avaya Article: Communication-Enabled Mashups: Empowering Both Business Owners and IT
    Intel Whitepaper: Building a Real-World Model to Assess Virtualization Platforms
    PDF: Intel Centrino Duo Processor Technology with Intel Core2 Duo Processor
    Microsoft Article: Build and Run Virtual Machines with Hyper-V Server 2008
      Go Parallel Article: Q&A with a TBB Junkie
    IBM Whitepaper: Innovative Collaboration to Advance Your Business
    Internet.com eBook: Real Life Rails
    IBM eBook: The Pros and Cons of Outsourcing
    Internet.com eBook: Best Practices for Developing a Web Site
    IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
    Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
    IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
    Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
    Symantec Whitepaper: Comprehensive Backup and Recovery of VMware Virtual Infrastructure
    MORE WHITEPAPERS, EBOOKS, AND ARTICLES
    webref The latest from WebReference.com Browse >
    Popular JavaScript Framework Libraries: An Overview - Part 3 · Accessing Your MySQL Database from the Web with PHP · Working with the DOM Stylesheets Collection
    Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
     The latest from internet.com
    Fixing MySQL Replication · Firewall Guide: First Steps to Securing the Enterprise · VoxOx Tames the Tumultuous Communications Tangle

    Created: May 13, 2005

    URL: