Javascript Data Type How to - Put single quotes around an array just "joined"








Question

We would like to know how to put single quotes around an array just "joined".

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>
<!--from w  w  w. j  a v a 2 s.c o m-->
function combine_ids(ids) {
   return (ids.length ? "'" + ids.join("','") + "'" : "");
}
console.log(combine_ids([]));
console.log(combine_ids([3]));
console.log(combine_ids([3, 4, 'a']));

</script>
</head>
<body>
</body>
</html>

The code above is rendered as follows: