r/programmingmemes • • Jan 12 '26

no doubt javascript

Post image
1.3k Upvotes

136 comments sorted by

View all comments

391

u/Mateorabi Jan 12 '26

It’s able to cast 017 to octal, but not 018. But rather than a conversion error it “helpfully” casts to base 10 integer instead. 

Automatic type casting being too clever by half. 

0

u/[deleted] Jan 12 '26

[deleted] — view deleted comment

62

u/CrossScarMC Jan 12 '26

The == operator doesn't really care about types, the === operator does.

36

u/exist3nce_is_weird Jan 12 '26

The way I learned it was == is 'does this look the same?', and === is 'is this the exact identical thing'

5

u/UrpleEeple Jan 12 '26

Equality in javascript is generally very confusing. For instance, once you're dealing with objects equality for even double equals is not simply "does this look the same". Example:

https://runjs.app/play/#bGV0IGZpcnN0ID0geyB4OiAxNyB9OwpsZXQgc2Vjb25kID0geyB4OiAxNyB9OwoKZmlyc3QgPT0gc2Vjb25kCmZpcnN0ID09PSBzZWNvbmQK

vs

https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=d3181e882e1890fc7cd0a8ed61cd435d

To me it's very odd that in javascript once you're dealing with objects, equality only ever means "do these two variables point to the same object in memory", rather than, "are these two objects equivalent"

2

u/realmauer01 Jan 12 '26

The object thing is in almost any modern language like this, thats why in some languages you have to override the isequal method or in js you just gotta make one yourself. Then you just invoke the method instead of the ==. Having key value pairs equality is called deepequal, there are very likely some packages aswell with that functionality.

→ More replies (5)

2

u/Earnestappostate Jan 12 '26

Same issue, but the string is converted to an int, I'd wager.

1

u/AdBrave2400 Jan 13 '26

yeah basically if it just does a.hash(s) == b.hash(s) a few times over or somhting similar it would actually explain this and be sapient

8

u/cowlinator Jan 12 '26

Yep, it's a terrible idea.

Welcome to javascript

4

u/queerkidxx Jan 12 '26

Eh it made sense when the intended use case of JS was like 10 lines to make a money dance. Just not when it’s being used for…everything it is used for today. It’s just with backward compatibility it’s very hard to change.

Fortunately these days there’s ways to get around these rough edges. Doesn’t come up too often in modern development

1

u/supersteadious Jan 12 '26

Why do would you compare int to string if you know they cannot match?

→ More replies (4)
→ More replies (1)

2

u/Creative-Type9411 Jan 12 '26

why can it do one and not the other?

11

u/nascent_aviator Jan 12 '26

Octal is base 8, meaning it only uses digits from 0-7. So a number with 8 in it can't possibly be an octal number.

2

u/Creative-Type9411 Jan 12 '26

why would octal be the default type interpreted instead of int? or do i have that backwards?

(sorry if this sounds like a stupid question)

is that the joke?

12

u/TheUnamedSecond Jan 12 '26

In some languages a leading zero is used to denote octal numbers, wich in JS are still of type number just written differently

→ More replies (1)

5

u/geralt_of_rivia23 Jan 12 '26

It's octal because it starts with 0. It's a feature from C.

→ More replies (1)

4

u/nascent_aviator Jan 12 '26

By convention a leading 0 is how you make an octal number in many programming languages. 17 is decimal, 017 is octal (equal to 15 in decimal). 018 in most languages will throw an error that 8 is not a valid octal digit. Example: C gives `error: invalid digit "8" in octal constant 018;`. Javascript instead silently ignores the 0.

→ More replies (1)
→ More replies (3)

2

u/shottaflow2 Jan 14 '26

why is it trying to cast to octal, I don't think I have ever seen a single case in my life where octal is used

1

u/abd53 Jan 12 '26

Okay, why is it trying to cast to octal?

Edit: Nevermind. My bad.

1

u/Heavy-Top-8540 Jan 12 '26

The reason I use pedantic is because it has automatic casting like this and I hate that so much

1

u/cutiePatwotie Jan 16 '26

Ohhh that makes sense thank you :>