@418teapot@lemmy.world
link
fedilink
English
4
edit-2
2M

Best I can do is

"\ude41🙂".split("").reverse().join("")

returns "\ude42🙁"

@RiceMunk@sopuli.xyz
link
fedilink
5
edit-2
2M

but

"🙂".reverse() == "🙃"

“:-)”.reverse() == “)-:”

Close enough

"🐈".concat() = "😼"
"☹️".reverse() == "☹️"
@hydroptic@sopuli.xyz
creator
link
fedilink
42M

You’re no fun

Look closer at the beauty mark, I flipped the emoji

@TCB13@lemmy.world
link
fedilink
English
12M

Today I found out that this is valid JS:

const someString = "test string";
console.log(someString.toString());
@hydroptic@sopuli.xyz
creator
link
fedilink
9
edit-2
2M

Everything that’s an Object is going to either inherit Object.prototype.toString() (mdn) or provide its own implementation. Like I said in another comment, even functions have a toString() because they’re also objects.

A String is an Object, so it’s going to have a toString() method. It doesn’t inherit Object’s implementation, but provides one that’s sort of a no-op / identity function but not quite.

So, the thing is that when you say const someString = "test string", you’re not actually creating a new String object instance and assigning it to someString, you’re creating a string (lowercase s!) primitive and assigning it to someString:

Compare this with creating a new String("bla"):

In Javascript, primitives don’t actually have any properties or methods, so when you call someString.toString() (or call any other method or access any property on someString), what happens is that someString is coerced into a String instance, and then toString() is called on that. Essentially it’s like going new String(someString).toString().

Now, what String.prototype.toString() (mdn) does is it returns the underlying string primitive and not the String instance itself:

Why? Fuckin beats me, I honestly can’t remember what the point of returning the primitive instead of the String instance is because I haven’t been elbow-deep in Javascript in years, but regardless this is what String’s toString() does. Probably has something to do with coercion logic.

[🌽].pop() == 🍿
@tetris11@lemmy.ml
link
fedilink
11
edit-2
2M
"🚴".push() = "🚲🤸"

JavaScript taking notes

Create a post

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

  • Posts must be relevant to programming, programmers, or computer science.
  • No NSFW content.
  • Jokes must be in good taste. No hate speech, bigotry, etc.
  • 1 user online
  • 5 users / day
  • 9 users / week
  • 108 users / month
  • 558 users / 6 months
  • 1 subscriber
  • 899 Posts
  • 3.11K Comments
  • Modlog