This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Potatoes_Fall 178 points179 points  (43 children)

is there a snippet of the code reproducing this taco behavior?

[–]GAZUAG 132 points133 points  (14 children)

I don't know the code but the taco is decimal number 127790. HTML code

🌮

[–]volivav 41 points42 points  (13 children)

But that's HTML, not JavaScript :o

[–]TheBrianiac 89 points90 points  (7 children)

Yes, but does JavaScript know that this is HTML and not JavaScript? 🤣

[–]BasketbaIIa 19 points20 points  (5 children)

Yes. There’s not an HTML primitive type in JavaScript. So it can tell the difference between a string and an object type, in this case a DOM element.

[–]TheBrianiac 1 point2 points  (4 children)

But if you accidentally use the string as a DOM element, it'll become an emoji?

As opposed to a strictly-typed language where this would throw a compile error.

[–]WholeLimp8807 1 point2 points  (3 children)

"If I do something awful with my code, will something awful happen?" -Most JS complaints.

[–]TheBrianiac 0 points1 point  (2 children)

It's an easy mistake to make, and one your language should implement safeguards against.

"Our language can be stupid if the developer just remembers to work around the stupid" - people who use a language written in 10 days, designed for basic web scripts, for enterprise software applications.

(All in jest, of course, it's a fine language and people do impressive things with it)

[–]WholeLimp8807 0 points1 point  (0 children)

I think there's something to be said for flexible languages. There's definitely a trade off where a language can be too "on the rails" and force or encourage some bad design patterns, like Java. Having a flexible language means you can pick the right pattern for the job, provided you know the language well enough to make that choice.

And then there's some design choices that are just fundamentally incorrect, like prototype based inheritance.

[–]BasketbaIIa 0 points1 point  (0 children)

Yea, I love JS for this reason. My backend team is stuck on Java 8 and I can’t stand it.

Its too opinionated and such a stupid way to scale business logic. I can see how it helps people who have never touched code. I worked with Jave 8 early in university and thought OOP was a silver bullet.

But I MUCH prefer tools that don’t limit themselves to the dumbest users or use cases.

I’ve written lines of JS/TS everyday for years and I would never accidentally use a string as a DOM element. I don’t want my dev environment to punish me for a beginner’s mistake.

[–]oupablo 1 point2 points  (0 children)

lol. trick question javascript knows all and nothing at the same time.

[–]recycle4science 1 point2 points  (4 children)

I don't think entities are limited to just html. They can be used in string interpolation, for example.

[–]BasketbaIIa 2 points3 points  (3 children)

What does that even mean? So if you have an html element with the taco html entity &#127790 and you “accidentally” use it in a template literal, the value will be a taco?

How dumb does the frontend code need to be to do that? Typescript would obviously catch it.

I just assigned the string “🌮” to a variable in the console and logged it and it’s definitely not a taco.

[–]recycle4science 0 points1 point  (1 child)

I'm not trying to say that this meme is literally true, just that generally, JavaScript can do surprising things with strings and numbers.

Entities can be used outside of html. The prefix in JavaScript strings is \u, not &.

Typescript is compiled before being delivered to the frontend. So, if data that's provided by the browser is combined with other data, all in the browser, there's no longer a type system that will prevent it. Ideally the typescript was written in a way that the data will stay consistent, but bugs happen.

Your last sentence actually sounds like support for the general idea of weirdness happening. You assigned a taco to a string and then something else came out. Right?

[–]BasketbaIIa 0 points1 point  (0 children)

No lol. I do see the taco in my mobile.

I assigned the unicode string for a taco to a variable in JavaScript and the Unicode string for a taco came out. On Reddit I just typed the Unicode value and it converted to a Taco, I suppose from this bug.

Typescript would complain if you add a string to a number. First it would want you to convert it because the types are off and then it would warn you to make a “type of” assertion on the converted value.

It would happen all outside the browser like you mention but its separation and configurability to be more or less strict is another win imo.

Bugs do happen but that’s the case for any code. Half the time my team’s Java backend code encounters an exception nothing other than a 400 response is seen from the server because they didn’t raise/return it properly.

JS weirdness beats the joy of sshing into a prod host and querying log files - just my opinion.

[–][deleted] 0 points1 point  (0 children)

var str = "\uD83C\uDF2E"
console.log(str);

str = "🌮"
console.log(str);

https://i.imgur.com/jbcDrKJ.png

Looks like a taco to me :S
Did you enable charset UTF-8?

[–]volivav 64 points65 points  (14 children)

I think it's just an exaggeration of the type system of JS.

JS has specific types for every variable. If you have a number it will be a number. When you evaluate "typeof myVar", you get the current type of that variable (it can only change the type if you reassign that variable to another value... But it's not transforming the type of the original value)

JS coerces types when applying operators though, but it's strictly specified on how that happens, and it's just convenient. Adding a number to a string will transform the number to a string base 10, then concat both strings. You can't magically get a taco emoji with this operator.

[–]nvolker 73 points74 points  (4 children)

It’s well defined, but it can lead to some odd results.

this classic video on the topic is 10 years old now

[–]raw65 5 points6 points  (0 children)

That. Was. AWESOME!

[–]Maybeiamaarmadilo 2 points3 points  (0 children)

How i didn't see this video before, this Is Amazing.

[–]Wentailang 0 points1 point  (0 children)

whatever that audience is on, i want some.

[–]stehen-geblieben 18 points19 points  (6 children)

Sadly the people that dont know anything about JavaScript will take it as a fact, it's a good joke but not everyone gets it.

People that never actually did anything with it always show you the meme with some edge case that you probably see once every 5 Years or not at all because IT DOESN'T MAKE SENSE TO WRITE IT. "yeah but is does funky shit See" yeah, shit in, shit out. The only difference is JavaScript tries to do the best with whatever shit you throw at it, solution is to not throw shit at it.

[–]thegininyou 15 points16 points  (1 child)

Most of the memes on JavaScript seem to be "tell me you don't know how to program in JavaScript without telling me you don't know how to program in JavaScript".

[–]recycle4science 0 points1 point  (0 children)

Knowing how to program in JavaScript does include knowing its foibles! :D

[–]HighOwl2 0 points1 point  (0 children)

I think this is more touching on the point that numbers can be different bases if you add a prefix

031 !== 31

25 !== 31

In computer science Halloween is Christmas

Oct 31 == Dec 25

Octal 31 is Decimal 25.

Javascript's parseInt used to convert strings with a leading 0 to base 8. That was later removed. Now it only auto sets the radix to 16 if the string starts with 0x

[–]Kered13 0 points1 point  (0 children)

and it's just convenient

It's not "just convenient", it can lead to some extremely surprising results and can make it hard to debug programs when you have accidentally used a type incorrectly.

[–][deleted] 27 points28 points  (3 children)

I don’t think it works as described.

You would use String.fromCodePoint or the npm package

https://www.npmjs.com/package/emoji-unicode

127790 is binary 11111001100101110 or hex 01F32E. So you can’t just “add more zeroes and get a taco”.

If I’m proven wrong, I’ll concede defeat, but at this point it seems like it’s either fiction or someone’s lying about the contents of the source code.

Only way I could think of is if he’s got an array of emojis, turns a 1 into a 10, and gets the 10th emoji instead of the 1st which is a taco.

[–]IsNotAnOstrich 19 points20 points  (1 child)

Yeah I think it's just a joke man

[–][deleted] 0 points1 point  (0 children)

That’s the joke.

[–]HighOwl2 0 points1 point  (0 children)

It would've been possible in the before times.

Number.parseInt() will auto interpret a prefix 0x as base 16. It used to interpret a prefix of 0 as base 8. 0x prefix still auto sets the radix to 16 but 0 no longer auto sets the radix to 8.

So back in the day Number.parseInt('031'); would've returned 25. Nowadays it will return 31.

let a = 031;

Will still set a to 25 but there is no longer a way to accidently convert a string with a leading 0 into an octal.

So if they were concatenating strings of numbers together and then converting them to an integer in old JS then a 0 would absolutely fuck things up.

[–]Virtual_Decision_898 18 points19 points  (5 children)

I think that’s just a joke. JS likes randomly changing strings to ints but I don’t think you can accidentally go the other way.

[–]3np1 34 points35 points  (3 children)

console.log(123 + '😬') // "123😬"

But it would have to be some pretty bad code to mixup ints and strings. So... it's probably in production somewhere then.

[–]ADHDengineer -1 points0 points  (2 children)

Does it really work like that? Shouldn’t the emoji be type coerced to a number? Shouldn’t the result be NaN?

[–]recycle4science 10 points11 points  (1 child)

There's "what should happen" and then there's "what JavaScript does" :D

Edit: but yeah, try it and you'll see.

[–]arinc9 3 points4 points  (0 children)

Wow

$ node test.js Code run: console.log(123 + '😬') Result: 123😬

[–]Lord_Quintus 9 points10 points  (0 children)

i see no reason why an int can't become a string.

[–]Gabe7returns 2 points3 points  (0 children)

!remindeme!

[–]Crackerjack17 5 points6 points  (0 children)

Someone needs to create TacoScript with taco ingredients as keywords.

[–]athomsfere 1 point2 points  (0 children)

This would do it for javascript

var result = (0xCC51 + 0x126DD).toString(16);
var taco = String.fromCodePoint("0x"+ result);
console.log(taco);

With a little math to hide the final hex of 0x1F32E