Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

71
Views
Spread operator in console and html element

Given an array of strings,

const bestRappers = ["Ice Cube", "MC Eiht", "Eazy-E"]
const output = document.getElementById('output')

/* 1: */ console.log(...bestRappers)
/* 2: */ output.innerText = [...bestRappers]
<p id="output"></p>

2 returns the items seperated by commas. 1 Isn´t seperated. Why?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

What a spread operator does is take an iterable(in your case array) and give it as n number of arguments.

In first case console.log is a function so it gives it 3 arguments console.log(...bestRappers) ---> console.log(Ice Cube,MC Eiht,Eazy-E) so generally console.log function logs every argument space separated

In second case you spread the array into an array again ([...bestRappers]) so when you assign array to an innerText it displays as comma separated values

about 4 years ago · Juan Pablo Isaza Report

0

If you would like the same output in your console like in your dom then you have to use the JavaScript join() function with comma as seperator.

working example

bestRappers = ["Ice Cube", "MC Eiht", "Eazy-E"];

console.log(bestRappers.join(', '));
output.innerText = [...bestRappers]
<p id="output"></p>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!