HelloWorld
HelloWorld

Reputation: 2330

Why is the split method still returning a string

I understand that javascript's split() method should take a string and split it into an array based on the parameter(s) passed in the method.

I have run the following in the console:

var sen = 'I love javascript';
sen.split(' ');
console.log(typeof(sen));

So split(' ') should split the string based on whitespace and return an array with 3 strings.

However the console returns the typeof as "string" rather than "object"

Does anyone know why?

Upvotes: 0

Views: 177

Answers (1)

jhinzmann
jhinzmann

Reputation: 988

Because split doesn't change sen. The returnvalue of

sen.split(' ');

would be an array. Try:

var sen = 'I love javascript';
var arr = sen.split(' ');
console.log(typeof(arr));

Upvotes: 3

Related Questions