125

# ðŸ™ƒ Javascript is beautiful ðŸ™ƒ

• 3
That's in C also! :)
• 11
Well, that works exactly as intended.
• 9
• 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
• 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.
• 3
@mikessoldier 0*** is Octal Notation. So every number that starts with a zero will be tried to be interpreted as an Octal number. Well except 0x***, which is hexadecimal.

Octal notation uses digits from 0 to 7. When you use 8, 9, it is no longer a valid octal number. Javascript thus sees it as a decimal number.
• 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.
• 4
@tylerleonhardt you sure? How do you set access to files in Linux?
• 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
• 4
@orijin that's a common thing for most, if not all programming languages. The thing is that floating point types are always approximations of the real value. In some cases this results in such situations. They are easy to spot. Just add numbers like *.3, *.2, *.7, to get to numbers like *.9 or *.7. You'll find loads of them.

Because of this, you should never compare floating point numbers with the equality operator, and always compute the absolute value of the difference and test it against a threshold.

For example, Math.abs(a-b)<0.00001
• 5
@orijin P.S.
That's why when dealing with situations that require high precision, platforms like Java or .NET use fixed point numbers like System.Decimal in .NET. They don't suffer from precision loss. In RDBMS they are presented as DECIMAL(n, p) or NUMERIC(n, p), where n is the total size of the number (how many digits plus the dot), and p is the precision (number of decimals). For example 162.34 would fit in NUMERIC(6,2).

So what about PHP and JavaScript, and maybe other platforms?
Well, a working approach is to work with Integers instead.
Option 1. When working with money, store and calculate everything not in dollars, but in cents. So price is not 6.99 but 699. An integer. Never lose precision again. When displaying, divide by 100. That's it.

Option 2. Make your own type or find a library that has this, which store 2 integers, one for the integer part, and the other for the decimals. Implement arithmetic operations, and you're set.
• 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?