Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript split function in chrome gives unexpected results

Tags:

javascript

Here is the piece of code

str = "a,b,c";
name = str.split(",");

The name variable shows up as 'object' type in Firefox and 'string' type in chrome Why is that happening ? Here is the jsfiddle http://jsfiddle.net/XujYT/17/

Also the name variable stores the value "a,b,c" instead of the split array in chrome http://jsfiddle.net/XujYT/23/

like image 409
Sethunath K M Avatar asked Dec 08 '22 19:12

Sethunath K M


1 Answers

Because name is a global variable used by chrome, and it’s not possible to override it without unexpected results. Try:

var name = str.split(","); // always use var for local variables!
like image 134
David Hellsing Avatar answered Jan 16 '23 14:01

David Hellsing