124

🙃 Javascript is beautiful 🙃

Comments
  • 3
    That's in C also! :)
  • 11
    Well, that works exactly as intended.
  • 9
    Try adding 0.1 to 0.2
  • 3
    Sorry for the question... But why? :S
  • 1
    Sorry for this huge lack of knowledge, but, Can someone explain me what is happening? Why does it says 511 instead of 777?
  • 0
    Same question as above.. just commenting to get notifications about it
  • 13
    @mikessoldier Leading 0 denotes that number is in octal (thus base 8) representation. ECMA 5th edition changed this (so leading zero doesn't denote octal but "regular" integer in base 10). /cc @ukabthebest
  • 26
    @mikessoldier He is using the octal system (base 8) by adding a 0 to the front. Octal only accepts numbers 0 to 7(afaik), so 777 is correctly put out as 511, but 888 is not an actual octal number, so javascript does its thing and seems to read it as decimal (base 10).

    @firusvg damn, i was too slow :P
  • 3
    Adding a 0 in front turns it into an octal. 777 in base 8 is 511. 888 is an invalid base 8 number, therefore the interpreter cuts off the first 0 and keeps it in base 10.
  • 3
    @MiracleMikuru But, actually, your comment is better in explaining what's going on.
  • 2
    Thanks for explaining everyone!

    Now the question is... Does anyone know why this exists within Javascript? Not sure about you, but I don't convert things to octal... Ever.
  • 2
    @tylerleonhardt you don't, but this doesn't mean no one else doesn't. Octal base is useful as a part of byte. There are use cases you need to operate on bitmasks, having a type for this is handy.
  • 0
    @apisarenco oops, I've been using octal all along :)
  • 0
    Thus js seems to be executed in a command line or a console, what is the console?
  • 0
    @abchin that was using the Node.js REPL that's built in to Node.

    The terminal was in iterm on a Mac but you can get to this REPL by installing Node and typing node in your terminal of choice.
  • 2
    @tylerleonhardt thanks a lot, your mention of the REPL helps me a lot. I would not struggle to debug front end or back end code any longer.
  • 0
    @abchin between that and the console in your browser of choice are the best options for interacting with js quickly and simply
  • 1
    @tylerleonhardt I approve of it.
  • 0
    What is up with that sum? :O why the extra digits? @DarekGreenly
  • 1
    0.1 + 0.2 == 0.3
    -> false

    @DarekGreenly, I love it
  • 0
    The same with Python for octal digits. Nice to know though
  • 0
    It seems like they could have come up with a better prefix for octal than zero.
  • 0
    @RingoMcKraken This will make a beginner mad :3
  • 0
    In what scenarios do you write numbers starting with zero other than strings?
Add Comment