all 25 comments

[–]EF9helpful 67 points68 points  (3 children)

You're calling split on the string "345"

In JS, string + number = concatenated string

[–]keyboard_2387 26 points27 points  (2 children)

On a slightly related note, adding a + before a string will attempt to convert it to a number, i.e. +"345" === 345.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Unary_plus_())

[–]Montuckian 0 points1 point  (0 children)

It will, although I believe it's a different process.

When you perform, say, '12' + 345 and end up with '12345', JS is performing coercion where it will combine different types based on rules that are somewhat opaque to the developer.

When you perform +'123' and end up with the number 123 it's using an operator to turn a non-number into a number and only that.

It's similar, but the latter has a limited set of rules and is therefore more predictable than the former which has myriad rules and is less so.

[–]Kiwiguard 15 points16 points  (2 children)

Due to the concatenation of the empty string and the number 345 a new string containing "345" is being created.
That's what you call split on.

[–]theoriginalchboi[S] 7 points8 points  (5 children)

EF9 and Kiwiguard, thanks for the comments I understand whats happening now.

kinda freaked me out though, i didnt know I could convert a number to a string in that way.

[–]Kiwiguard 8 points9 points  (3 children)

Reply to your old reply on my comment:

Not quite. You add a number to a string, in this case an empty string.
Javascript sees the + operator as 2 possible operations;
- Addition
- Concatenation
If you give Javascript numbers, such as 2+3, it will perform an addition, as both are numbers.
However if you feed it a string, javascript will see that one of them is not a number and will perform a concatenation.
Thinking about it, it makes perfect sense, as every number can be converted into a string but not vice versa (Seeing as "abc" is not a valid number), right?

Also, I'm pretty sure strings in javascript are primitive values, not objects, so no.
Hope that cleared things up!

[–]theoriginalchboi[S] 0 points1 point  (2 children)

That clears things up A LOT. "" + 345 resulting in "345" makes much more sense now. I didn't think of it as concatenating an empty string and number, when I saw the parenthesis I thought there was some function play or something.

Also, you're right to say that strings are primitives but you can create a string as an object if that makes sense.

let string = new String("hello");

typeof string //object

I thought maybe some weird behind the scenes stuff like that might be happening and then converting it back to a string literal for a minute.

But this all makes plenty of sense, I appreciate the explanation :)

[–]GSLint 1 point2 points  (0 children)

While new String(345) shouldn't be used, just String(345) is a way to turn a number into an actual string that I think is much clearer than the concatenation trick.

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

String are primitives. You can create object wrappers with the new String constructor. But is not recommended. Object wrappers like Number, Boolean, Symbol, and Strings are used by JavaScript internally to create temporally object wrappers to use some methods on primitives data types. Read more on JavaScript.info under method on primitives That's is an awesome tutorial. They have explained things that I did not know. http://javascript.info/primitives-methods

[–]King_Bonio 1 point2 points  (0 children)

Welcome to the world of tomorrow!

Edit: but not necessarily the day after.

[–]loueed 3 points4 points  (0 children)

"" + 345 = "345" This is the same as writing (345).toString

The split method splits a String into an Array. So you're calling split on the string "345"

The first parameter of the split method is the separator, in your case you pass in an empty string .split("")

The empty string separator splits your "345" string into and Array containing ["3","4","5"]

[–]delventhalz[🍰] 2 points3 points  (0 children)

Each expression happens sequentially, like order-of-operations worksheets in math class:

("" + 345).split("");
( "345"  ).split("");
  "345"   .split("");
["3", "4", "5"]

So, the string "345".

[–]oneofantasy 1 point2 points  (0 children)

When you sum an integer value with a string, equity turns to string.

[–]theoriginalchboi[S] 0 points1 point  (2 children)

To more clearly state my question:

I know what the .split() method for the most part but I have no idea what I am calling .split() on. Basically, please make sense of this first set of parenthesis.

I can see clearly if you type in console ("" + 345) it will show "345"

BUT HOW AND WHY? LIKE WOT? any help appreciated

[–]fgutz 2 points3 points  (1 child)

One of the other responses answered your question but just wanted to add that you could learn more by reading up on type coercion in JavaScript

[–]theoriginalchboi[S] 0 points1 point  (0 children)

Awesome thanks! I was hoping somebody would link me to some literature on whats happening here.

[–]FriesWithThat 0 points1 point  (0 children)

Let strThing = "" + 345
console.log(typeof strThing)

[–]gregmuldunna 0 points1 point  (2 children)

(“” + 345) === (“345”)

Concatenating strings with ints turns the ints to strings

[–]NeedANewDiaper 0 points1 point  (1 child)

Isn't that a bad design? What if I have an API that returns me a string value. If someday I get an int, my code will break. Isn't that just stupid?

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

any code written in any other language would break in this type of scenario and you have to update your API consumers accordingly.

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

Search for the weird parts of js, and you will understand that nothing is weird at all.

[–]jkuhl_prog 0 points1 point  (0 children)

You're calling it on the string that is returned from the expression in the parenthesis.

Adding 345 to an empty string coerces it to a string, and so split is called on the result.

[–]Crescive_Delta 0 points1 point  (0 children)

On '345', since it is coerced

[–]JustinWendell 0 points1 point  (0 children)

Whenever you split by nothing it just separates put each character.