1

there is a line. how do I split it into an array via separator ",". If you try to split, the same line is returned

var str = "1,2,3,4,5,6";
console.log(str);
console.log(typeof(str));

var name = str.split(",");

console.log(name);
console.log(typeof(name));
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320

1 Answers1

0

name is a global property of Window object that is accessible throughout the JavaScript runtime and this is always a String type so despite of str.split(',') returning array of elements, due to its default type it is converted back to the string values. Thus, you need to use different variable name say, _name.

var str = "1,2,3,4,5,6";
console.log(str);
console.log(typeof(str));

var _name = str.split(',');
console.log(_name);
console.log(typeof(_name));
Ankit Agarwal
  • 30,378
  • 5
  • 37
  • 62