in JS why does event.target give different results based on if its in a template literal or added to the string

I was following lectures on JS, we created a random number generator, then used that to create a random rgb color function.

We then added an eventlistener to a link element in the DOM, and inside the callback we have a console.log and the color generator call.

in this console.log i use first template literal to post event.target, i then outside the template literal again post the event.target.

so my question is this: why does the event.target inside the template literal post a link to the …/index.html#section-1 While the one outside post the element that was clicked?

what is happening here for them to give different results? i usually just use template literals whenever i post to console, but i have to rethink this practice now as it seems to be a difference.

CLICK features link to see what i am talking about.

const randomNumber = (min, max) => Math.floor(Math.random() * max) + min;

const randomColor = () =>
  `rgb(${randomNumber(0, 255)},${randomNumber(0, 255)},${randomNumber(
    0,
    255
  )})`;

document.querySelector('.nav__link').addEventListener('click', function (e) {
  console.log(`linkn ${e.target}n`, e.target);
  this.style.backgroundColor = randomColor();
});
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<link rel="shortcut icon" type="image/png" href="img/icon.png" />
<link
href="https://fonts.googleapis.com/css?family=Poppins:300,400,500,600&display=swap"
rel="stylesheet"
/>
<link rel="stylesheet" href="style.css" />
<title>page</title>
</head>
<body>
<header class="header">
<nav class="nav">
<img
src="img/logo.png"
alt="logo"
class="nav__logo"
id="logo"
designer="JS"
data-version-number="3.0"
/>
<ul class="nav__links">
<li class="nav__item">
<a class="nav__link" href="#section--1">Features</a>
</li>
<li class="nav__item">
<a class="nav__link" href="#section--2">Operations</a>
</li>
<li class="nav__item">
<a class="nav__link" href="#section--3">Testimonials</a>
</li>
<li class="nav__item">
<a class="nav__link nav__link--btn btn--show-modal" href="#"
>Open account</a
>
</li>
</ul>
</nav>
<div class="header__title">
<h1>
h1
</h1>
<h4>h4</h4>
<button class="btn--text btn--scroll-to">Learn more &DownArrow;</button>
<img
src="img/hero.png"
class="header__img"
alt="items"
/>
</div>
</header>
<section class="section" id="section--1">
<div class="section__title">
<h2 class="section__description">Features</h2>
<h3 class="section__header">
h3.
</h3>
</div>
<div class="features">
<img
src="img/digital-lazy.jpg"
data-src="img/digital.jpg"
alt="Computer"
class="features__img lazy-img"
/>
<div class="features__feature">
<div class="features__icon">
<svg>
<use xlink:href="img/icons.svg#icon-monitor"></use>
</svg>
</div>
<h5 class="features__header">100% digital </h5>
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Unde alias
sint quos? Accusantium a fugiat porro reiciendis saepe quibusdam
debitis ducimus.
</p>
</div>
<div class="features__feature">
<div class="features__icon">
<svg>
<use xlink:href="img/icons.svg#icon-trending-up"></use>
</svg>
</div>
<h5 class="features__header">Some text</h5>
<p>
Nesciunt quos autem dolorum voluptates cum dolores dicta fuga
inventore ab? Nulla incidunt eius numquam sequi iste pariatur
quibusdam!
</p>
</div>
<img
src="img/grow-lazy.jpg"
data-src="img/grow.jpg"
alt="Plant"
class="features__img lazy-img"
/>
<img
src="img/card-lazy.jpg"
data-src="img/card.jpg"
alt="Credit card"
class="features__img lazy-img"
/>
<div class="features__feature">
<div class="features__icon">
<svg>
<use xlink:href="img/icons.svg#icon-credit-card"></use>
</svg>
</div>
<h5 class="features__header">Some text</h5>
<p>
Quasi, fugit in cumque cupiditate reprehenderit debitis animi enim
eveniet consequatur odit quam quos possimus assumenda dicta fuga
inventore ab.
</p>
</div>
</div>
</section>
<section class="section" id="section--2">
<div class="section__title">
<h2 class="section__description">Operations</h2>
<h3 class="section__header">
Everything as simple as possible, but no simpler.
</h3>
</div>
<div class="operations">
<div class="operations__tab-container">
<button
class="
btn
operations__tab operations__tab--1 operations__tab--active
"
data-tab="1"
>
<span>01</span>Instant 
</button>
<button class="btn operations__tab operations__tab--2" data-tab="2">
<span>02</span>Instant2
</button>
<button class="btn operations__tab operations__tab--3" data-tab="3">
<span>03</span>Instant2
</button>
</div>
<div
class="
operations__content
operations__content--1
operations__content--active
"
>
<div class="operations__icon operations__icon--1">
<svg>
<use xlink:href="img/icons.svg#icon-upload"></use>
</svg>
</div>
<h5 class="operations__header">
no BS.
</h5>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim
ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut
aliquip ex ea commodo consequat.
</p>
</div>
<div class="operations__content operations__content--2">
<div class="operations__icon operations__icon--2">
<svg>
<use xlink:href="img/icons.svg#icon-home"></use>
</svg>
</div>
<h5 class="operations__header">
Buy a home or make your dreams come true.
</h5>
<p>
Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat
cupidatat non proident, sunt in culpa qui officia deserunt mollit
anim id est laborum.
</p>
</div>
<div class="operations__content operations__content--3">
<div class="operations__icon operations__icon--3">
<svg>
<use xlink:href="img/icons.svg#icon-user-x"></use>
</svg>
</div>
<h5 class="operations__header">
No longer need? No problem!
</h5>
<p>
Excepteur sint occaecat cupidatat non proident, sunt in culpa qui
officia deserunt mollit anim id est laborum. Ut enim ad minim
veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex
ea commodo consequat.
</p>
</div>
</div>
</section>
<section class="section" id="section--3">
<div class="section__title section__title--testimonials">
<h2 class="section__description">Not sure yet?</h2>
<h3 class="section__header">
Making their lifes simpler.
</h3>
</div>
<div class="slider">
<div class="slide slide--1">
<div class="testimonial">
<h5 class="testimonial__header">Best decision ever!</h5>
<blockquote class="testimonial__text">
Lorem ipsum dolor sit, amet consectetur adipisicing elit.
Accusantium quas quisquam non? Quas voluptate nulla minima
deleniti optio ullam nesciunt, numquam corporis et asperiores
laboriosam sunt, praesentium suscipit blanditiis. Necessitatibus
id alias reiciendis, perferendis facere pariatur dolore veniam
autem esse non voluptatem saepe provident nihil molestiae.
</blockquote>
<address class="testimonial__author">
<img src="img/user-1.jpg" alt="" class="testimonial__photo" />
<h6 class="testimonial__name">Aarav Lynn</h6>
<p class="testimonial__location">San Francisco, USA</p>
</address>
</div>
</div>
<div class="slide slide--2">
<div class="testimonial">
<h5 class="testimonial__header">
The last step to becoming a complete minimalist
</h5>
<blockquote class="testimonial__text">
Quisquam itaque deserunt ullam, quia ea repellendus provident,
ducimus neque ipsam modi voluptatibus doloremque, corrupti
laborum. Incidunt numquam perferendis veritatis neque repellendus.
Lorem, ipsum dolor sit amet consectetur adipisicing elit. Illo
deserunt exercitationem deleniti.
</blockquote>
<address class="testimonial__author">
<img src="img/user-2.jpg" alt="" class="testimonial__photo" />
<h6 class="testimonial__name">Miyah Miles</h6>
<p class="testimonial__location">London, UK</p>
</address>
</div>
</div>
<div class="slide slide--3">
<div class="testimonial">
<h5 class="testimonial__header">
Finally free
</h5>
<blockquote class="testimonial__text">
Debitis, nihil sit minus suscipit magni aperiam vel tenetur
incidunt commodi architecto numquam omnis nulla autem,
necessitatibus blanditiis modi similique quidem. Odio aliquam
culpa dicta beatae quod maiores ipsa minus consequatur error sunt,
deleniti saepe aliquid quos inventore sequi. Necessitatibus id
alias reiciendis, perferendis facere.
</blockquote>
<address class="testimonial__author">
<img src="img/user-3.jpg" alt="" class="testimonial__photo" />
<h6 class="testimonial__name">Francisco Gomes</h6>
<p class="testimonial__location">Lisbon, Portugal</p>
</address>
</div>
</div>
<button class="slider__btn slider__btn--left">&larr;</button>
<button class="slider__btn slider__btn--right">&rarr;</button>
<div class="dots"></div>
</div>
</section>
<section class="section section--sign-up">
<div class="section__title">
<h3 class="section__header">
The best day to join was one year ago. The second best is
today!
</h3>
</div>
<button class="btn btn--show-modal">Open your account today!</button>
</section>
<footer class="footer">
<ul class="footer__nav">
<li class="footer__item">
<a class="footer__link" href="#">About</a>
</li>
<li class="footer__item">
<a class="footer__link" href="#">Pricing</a>
</li>
<li class="footer__item">
<a class="footer__link" href="#">Terms of Use</a>
</li>
<li class="footer__item">
<a class="footer__link" href="#">Privacy Policy</a>
</li>
<li class="footer__item">
<a class="footer__link" href="#">Careers</a>
</li>
<li class="footer__item">
<a class="footer__link" href="#">Blog</a>
</li>
<li class="footer__item">
<a class="footer__link" href="#">Contact Us</a>
</li>
</ul>
<img src="img/icon.png" alt="Logo" class="footer__logo" />
<p class="footer__copyright">
&copy; Copyright by
</p>
</footer>
<div class="modal hidden">
<button class="btn--close-modal">&times;</button>
<h2 class="modal__header">
Open your account <br />
in just <span class="highlight">5 minutes</span>
</h2>
<form class="modal__form">
<label>First Name</label>
<input type="text" />
<label>Last Name</label>
<input type="text" />
<label>Email Address</label>
<input type="email" />
<button class="btn">Next step &rarr;</button>
</form>
</div>
<div class="overlay hidden"></div>
<script src="script.js"></script>
</body>
</html>

Answer

It’s the difference between e.target and e.target.toString()

That is how console.log works when you concatenate a string to an object that has a toString – sometimes you will get yourString [object Object]

const randomNumber = (min, max) => Math.floor(Math.random() * max) + min;
const randomColor = () =>
`rgb(${randomNumber(0, 255)},${randomNumber(0, 255)},${randomNumber(
0,
255
)})`;
document.querySelector('.nav__link').addEventListener('click', function(e) {
console.log(e.target,e.target.toString())
console.log(`linkn ${e.target}n`, e.target);
this.style.backgroundColor = randomColor();
});
<a class="nav__link" href="#section--1">Features</a>