how to convert an array to string in javascript code example

Example 1: js array to string

var myArray = ['no','u'];
var myString = myArray.toString();

Example 2: javascript convert in a string the items of an array

const arr = [1, 2, 'a', '1a'];
const str = arr.toString();
console.log(str); //> "1,2,a,1a"

Example 3: transform array to string js

//Use array.join(separator) //
const myArray = [1, 2, 3, 4, 5, 6];
console.log(a.join('/'));
//output : 1/2/3/4/5/6
const myArray = ['g', 'o', 'o', 'g', 'l', 'e'];
console.log(a.join(''));
//output : 'google'
// Source : https://www.geeksforgeeks.org/javascript-array-join-method/#:~:text=Below%20is%20the%20example%20of%20the%20Array%20join()%20method.&text=The%20arr.,is%20a%20comma(%2C%20).

//Other function : array.toString() //
const array1 = [1, 2, 'a', '1a'];
console.log(array1.toString());
// output: "1,2,a,1a"
// Source : https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux/Array/toString

Example 4: how to print a array js

var array = [1,2,3]

for(var i = 0; i < array.length ; i++){
    console.log(array[i])
}

Example 5: array to string javascript

<array>.join(<splitting character(s)>);

Example 6: array to string

/**
 * Array.prototype.toString() method
 *
 * Returns a string representing the elements of the array. Arguments are not
 * expected and silently ignored. Utilized under the hood when an array are
 * referred like a string should.
 */
var array = [1, 2, 3, 'I', 'II', 'III', 'ūnus', 'duo', 'trēs']

/**
 * Expected output:
 * '1,2,3,I,II,III,ūnus,duo,trēs'
 * '1,2,3,I,II,III,ūnus,duo,trēs'
 * '1,2,3,I,II,III,ūnus,duo,trēs'
 */
console.log(
	'%s\n%s\n%s',
    array.toString(),
    String(array),
    array + ''
)

/**
 * Under the hood Array.prototype.toString() utilizes Array.prototype.join()
 * method if available and falls back to Object.prototype.toString() method
 * otherwise
 * 
 * Expected output:
 * none if Array.prototype.toString() utilizes Array.prototype.join() method,
 * 'Assertion failed: [1,2,3,"I","II","III","ūnus","duo","trēs"].toString()
 * utilizes fallback method Object.prototype.toString() instead of the default
 * .join()' otherwise
 */
console.assert(
    typeof array.toString === typeof array.join &&
    array.toString() === array.join(),
    '%o.%O utilizes fallback method %s.%O instead of the default .%O',
    array,
    array.toString,
    'Object.prototype',
    Object.prototype.toString,
    array.join || Array.prototype.join
)

/**
 * Expected output:
 * '1,2,3,I,II,III,ūnus,duo,trēs'
 * '[object Array]'
 */
console.log(
    '%s\n%s',
    array.toString(),
    (array.join = null) || array.toString()
)

/**
 * Kinda polyfill with barbaric static fallback if Array.prototype.join()
 * and Object.prototype.toString() methods are both unavailable
 */
Array.prototype.toString || 
    Object.defineProperty(Array.prototype, 'toString', {
        value : function toString () {
            return typeof this.join === 'function'
            	? this.join()
                : typeof Object.prototype.toString === 'function'
                	? Object.prototype.toString.call(this)
                    : '[object Array]'
        },
        configurable: true,
        writable: true
   })