Home > Net >  How to add 2 strings to an array and display one down the other
How to add 2 strings to an array and display one down the other

Time:04-06

I have two strings i need to add those to the array and display one down the other

const str1 = "SamsungGalaxy A52"
const str2 = "SamsungGalaxy A53"

let arr=[]

arr.push(str1,str2)

and I want to display like

  SamsungGalaxy A52
  SamsungGalaxy A53

CodePudding user response:

If you want to push two values to an array, you can do it with arr.push(str1, str2); or arr.push(str1); arr.push(str2);

You could create a ul & li list to display this list in UI.

const str1 = "SamsungGalaxy A52"
const str2 = "SamsungGalaxy A53"

let arr=[]

arr.push(str1);
arr.push(str2);
// arr.push(str1, str2);

console.log(arr);

const ul = document.createElement('ul');

for (const item of arr) {
  const li = document.createElement('li');
  li.textContent = item;
  ul.append(li);
}

document.querySelector('body').appendChild(ul)

CodePudding user response:

You can try something like it:

const product_1 = "SamsungGalaxy A52";
const product_2 = "SamsungGalaxy A53";

let products = [];

products.push(product_1, product_2);

const ul = document.querySelector('ul');

for (const product of products) {
  const li = document.createElement('li');
  li.textContent = product;
  ul.append(li);
}
li {
  list-style: none;
}
<ul>
</ul>

  • Related