Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is causing the error `string.split is not a function`?

People also ask

What is split () function in string?

The split() method splits a string into an array of substrings. The split() method returns the new array. The split() method does not change the original string. If (" ") is used as separator, the string is split between words.

Is split () a function?

The split() function can be used to split a given string or a line by specifying one of the substrings of the given string as the delimiter. The string before and after the substring specified as a delimiter is returned as the output.

What does split return if character not found?

split (separator, limit) , if the separator is not in the string, it returns a one-element array with the original string in it.

Does split () alter the original string?

Note: The split() method does not change the original string. Remember – JavaScript strings are immutable. The split method divides a string into a set of substrings, maintaining the substrings in the same order in which they appear in the original string. The method returns the substrings in the form of an array.


Change this...

var string = document.location;

to this...

var string = document.location + '';

This is because document.location is a Location object. The default .toString() returns the location in string form, so the concatenation will trigger that.


You could also use document.URL to get a string.


maybe

string = document.location.href;
arrayOfStrings = string.toString().split('/');

assuming you want the current url


run this

// you'll see that it prints Object
console.log(typeof document.location);

you want document.location.toString() or document.location.href


document.location isn't a string.

You're probably wanting to use document.location.href or document.location.pathname instead.


In clausule if, use (). For example:

stringtorray = "xxxx,yyyyy,zzzzz";
if (xxx && (stringtoarray.split(',') + "")) { ...