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/
asked Nov 17, 2012 at 9:35
Sethunath K M
4,7694 gold badges32 silver badges42 bronze badges
1 Answer 1
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!
answered Nov 17, 2012 at 9:37
David Hellsing
109k44 gold badges181 silver badges214 bronze badges
Sign up to request clarification or add additional context in comments.
6 Comments
Sethunath K M
@David Wow ! I never knew that . Any Idea what is that variable used for ?
GottZ
@Sethunath: any reason not using
var in the first place?Sethunath K M
@Jan-StefanJanetzky no .
var name would do . I was just curious to know what was going onDavid Hellsing
@Sethunath the global
name variable is not meant to be manipulated, and it doesn’t behave like regular variables. F.ex, if you assign something to it it will evaluate that value as toString(), so name={} will evaluate to "[object Object]". |
lang-js