Javascript split function in chrome gives unexpected results

Here is a snippet of code

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

The variable name displayed as the type "object" in Firefox and the type "string" in chrome Why is this happening? Here is jsfiddle http://jsfiddle.net/XujYT/17/

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

+4
source share
1 answer

Because name is a global variable used by chrome and cannot be overridden without unexpected results. Try:

 var name = str.split(","); // always use var for local variables! 
+14
source

Source: https://habr.com/ru/post/1446618/


All Articles