www.webdeveloper.com
Results 1 to 8 of 8

Thread: Javascript number alerts

  1. #1
    Join Date
    Apr 2010
    Location
    Charlotte,NC
    Posts
    4

    Thumbs up Javascript number alerts

    While working on a javascript issue for our project, we notice the strange behavior of integers and strings.We were about to validate numbers that starts with 000123852 as our application stores the product id like this.while validation for the overall length of 9, the value suddenly changes to a random number 342391.

    Later we identified alert(000123456); in the javascript console, we will find a random number 42798 .Finally we passes the numbers as strings.

    Can anyone please explain, why this mysterious behavior happens.

    regards,
    Jude
    http://www.genlinux.org
    Last edited by judearasu; 10-05-2011 at 12:12 PM. Reason: Did provide the spec

  2. #2
    Join Date
    Apr 2010
    Location
    Salem,Ma
    Posts
    623
    show your code.

  3. #3
    Join Date
    Apr 2010
    Location
    Charlotte,NC
    Posts
    4
    just type this in any javascript console

    alert(000123756);

    output seems to be 3546;

  4. #4
    Join Date
    Apr 2010
    Location
    Salem,Ma
    Posts
    623
    the numbers should be in qoutation marks. by doing it without them it applying some kind of calculation, very odd

  5. #5
    Join Date
    Apr 2010
    Location
    Charlotte,NC
    Posts
    4
    Can you have any idea behind the calculation ?

  6. #6
    Join Date
    Apr 2010
    Location
    Salem,Ma
    Posts
    623
    no idea, btu the bottom line is the alert is being used incorrectly. if you want to display the exact result then use qoutation marks.

  7. #7
    Join Date
    Jan 2007
    Location
    Wisconsin
    Posts
    2,120
    The leading 0 causes the browser to attempt to parse it as an octal number: http://en.wikipedia.org/wiki/JavaScript_syntax#Number
    Jon Wire

    thepointless.com | rounded corner generator

    I agree with Apple. Flash is just terrible.

    Use CODE tags!

  8. #8
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,253
    Yes it's the reason. An alert((((((1*8+2)*8+3)*8)+4)*8+5)*8+6) give 42798 witch isn't a random number !

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles