Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. JavaScript
  4. Comparison's in JavaScript

Comparison's in JavaScript

Scheduled Pinned Locked Moved JavaScript
javascriptquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    BobInNJ
    wrote on last edited by
    #1

    I would expect the following two expressions to both evaluate to false: 2 < "12" "2" < "12" The second one returns false. That makes sense to me "1" is before "2". In the first case, we are comparing a number with a string. I would expect the number 1 to be converted to the string "1" and therefore to evaluate to false. However, it is evaluating to true. What am I missing? Thanks, Bob

    Z A 2 Replies Last reply
    0
    • B BobInNJ

      I would expect the following two expressions to both evaluate to false: 2 < "12" "2" < "12" The second one returns false. That makes sense to me "1" is before "2". In the first case, we are comparing a number with a string. I would expect the number 1 to be converted to the string "1" and therefore to evaluate to false. However, it is evaluating to true. What am I missing? Thanks, Bob

      Z Offline
      Z Offline
      ZurdoDev
      wrote on last edited by
      #2

      BobInNJ wrote:

      it is evaluating to true

      BobInNJ wrote:

      2 < "12"

      12 is getting converted to int.

      Social Media - A platform that makes it easier for the crazies to find each other. Everyone is born right handed. Only the strongest overcome it. Fight for left-handed rights and hand equality.

      1 Reply Last reply
      0
      • B BobInNJ

        I would expect the following two expressions to both evaluate to false: 2 < "12" "2" < "12" The second one returns false. That makes sense to me "1" is before "2". In the first case, we are comparing a number with a string. I would expect the number 1 to be converted to the string "1" and therefore to evaluate to false. However, it is evaluating to true. What am I missing? Thanks, Bob

        A Offline
        A Offline
        Anandkumar Prajapati
        wrote on last edited by
        #3

        Comparing data of different types may give unexpected results. When comparing a string with a number, JavaScript will convert the string to a number when doing the comparison. When comparing two strings, "2" will be greater than "12", because (alphabetically) 1 is less than 2.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups