0

I have a string, I just want the first half, before the &. So naturally, you would think this would work...

 var string = "EAAzqcZChQwMt3eJjGTucj1Illzr2v1oJsIY1NHulzZDZD&expires_in=4994";
 string.split("&");
 console.log(string[0]);

You would think string[0] would return

  EAAzqcZChQwMt3eJjGTucj1Illzr2v1oJsIY1NHulzZDZD

But it returns

  E

What gives? Hopefully Im just being an idiot, and need to go to bed soon. But I can't see whats wrong here.

Kylie
  • 11,421
  • 11
  • 47
  • 78

1 Answers1

3

String#split returns array, you are suppose to access value by passing index

In your example, you are accessing the first-character from variable string which is E

var string = "EAAzqcZChQwMt3eJjGTucj1Illzr2v1oJsIY1NHulzZDZD&expires_in=4994";
var split = string.split("&");
console.log(split[0]);
Rayon
  • 36,219
  • 4
  • 49
  • 76
  • Isnt that what Im doing with [0]. Thats the index I want access to. The first half of the string? – Kylie Aug 17 '16 at 05:43
  • 10
    You're too tired, go to bed. :) – Will Aug 17 '16 at 05:44
  • @KyleK – `split` returns a new value, it does not manipulate existing `string` variable.. – Rayon Aug 17 '16 at 05:44
  • @KyleK note the different variables **string** and **split** – Isaac Aug 17 '16 at 05:45
  • 1
    Thanks, I dont know whos answer to accept, since technically you're both correct. Maybe I should just delete this question altogether, cause it makes me look like a complete idiot. lol – Kylie Aug 17 '16 at 05:47
  • 1
    @KyleK By `string[0]` you were indexing the first character of the original `string`, classic :-D – Peter Chaula Aug 17 '16 at 05:48