Convert to string without removing whitespace after every comma

Viewed 71

I have one requirement which needs my string to allow whitespace after every comma.My Array looks like this ['yes', 'no']. While converting this to string it became in the order of yes,no but I want the output to be like this yes, no allowing whitespaces after every comma.

let array=['yes', 'no'];
let result = array.toString().substring(0, (array.toString()).length).split(",");
console.log(result);

Answer was like yes,no but I need it to be yes, no. Is the any way by which I can get this output as a result.

4 Answers

let array=['yes', 'no'];
console.log(array.join(', '))

Try

    let array=['yes', 'no'];
    let result = array.toString().substring(0, (array.toString()).length).split(",").join(', ');
    console.log(result);
let array=['yes', 'no'];
let result = array.join(', ');
console.log(result); // Outputs - "yes, no"
$(document).ready(function () {
  let array = [" yes", "   no"];
  var i = "";
  $.each(array, function (index, value) {
    i = i + value;
  });
  console.log(i);
});
Related