How to print array of string in JavaScript

Create a program to print an array of strings using the Javascript function. Let’s create a simple web page that displays strings of arrays using  JS.

Problem Statement

In this, we have to create an array and print the array of strings using the javascript function. For that create an HTML file and use the JS function that displays the output.

Solution

  • Create HTML file
  • Create heading as Print array of strings in <h2></h2> tag
  • Display array in bold tag <b></b>,  <b> The array is:[1, 4, ‘Shravan’, ‘Nihan’, ‘Leyla’, 3, 9,’Doctor’]</b><br/><br/>
  • In javascript create original array using  const arr = [1, 4, ‘Shravan’, ‘Nihan’, ‘Leyla’, 3, 9,’Doctor’];
  • Convert array to string using const str = arr.toString();
  • Print array of string document.write(“ArrayofString: “+ str);

JS code to print array of string using javascript function

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Progrsm to create and print array of strings</title>
</head>
<body style="text-align: center;">
    <h2 style="color: blue;">Print array of string</h2>
     <b> The array is:[1, 4, 'Shravan', 'Nihan', 'Leyla', 3, 9,'Doctor']</b><br/><br/>

<script type="text/javascript">
    const arr = [1, 4, 'Shravan', 'Nihan', 'Leyla', 3, 9,'Doctor'];
const str = arr.toString();
document.write("ArrayofString:  "+ str); 
</script>
</body>
</html>

Output

JS program to print an array of strings using an array.values()

Problem statement

In this task create a program to print an array of strings using an array. values() in javascript. For that create an HTML file and use the JS function that displays the output.

Solution

  • Create HTML file
  • Enter heading in <b></b> tag as “Print array of string using array.values()”
  • Initialize array using var array = [ ‘a’, ‘m’,’B’,’s’,’D’,’get’];  print array using  document.write(“Array of string is -“+ array+”</br>”); 
  • Call the function array.values() using var iterator = array.values(); 
  • Print all elements of array string using for loop for (let elements of iterator){ document.write(“</br>”+elements); }Out

JS code to print an array of string using array.values()

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Print array of string</title>
</head>
<h2><b>Print array of string using array.values()</b></h2>
<body>
<script type="text/javascript">
var array = [ 'a', 'm','B','s','D','get'];
 document.write("Array of string is -"+ array+"</br>");
var iterator = array.values();
 	document.write("Printed array is ");

for (let elements of iterator) {
  document.write("</br>"+elements);
}

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

Output

In this way, we learn how to create and print an array of strings using JS functions.