It is completely safe to do this in all browsers. There is no risk involved.
When you assign the variable arr
to something new, it doesn't matter what it was previously referring to. (This action does not actually "overwrite the older array", but if there are no other references to the previous array, the garbage collector will handle it.)
You can achieve the same result in a single line of code:
arr=str.split(" ")[0].split("/");
Keep in mind that .split()
always returns an array with at least one element, even if the original string was empty or did not contain the specified separator.
UPDATE: If both the source string and the separator are empty strings, .split()
appears to return an empty array. For example, "".split("")
results in []
. Thanks to Munim for bringing this to light. (However, "".split(" ")
gives [""]
, so there should be no issue for the purpose of this discussion.)