/    Sign up×
Community /Pin to ProfileBookmark

Should I need at a semicolon at the end of “addEvenListener”?

Hello,

Should I add a semicolon at the end of the line I quoted as below?

>

divs[1].addEventListener(‘click’, fn)

I’ve tested both work. What’s the standard about this writing?

Thank you!

[code]
<!DOCTYPE html>
<html lang=”en”>
<head>
<meta charset=”UTF-8″>
<meta name=”viewport” content=”width=device-width, initial-scale=1.0″>
<title>Document</title>
<style>
div {
width: 100px;
height: 100px;
margin: 10px auto;
background-color: pink;
}
</style>
</head>
<body>
<div>1</div>
<div>2</div>
<div>3</div>
<script>
var divs = document.querySelectorAll(‘div’);
divs[0].onclick = function (){
alert(‘hello’);
divs[0].onclick = null;
}
divs[1].addEventListener(‘click’, fn)
function fn(){
alert(‘I am two’);
divs[1].removeEventListener(‘click’,fn);
}

</script>
[/code]

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERMar 19.2020 — Technically, no,

but it is considered good syntax in reading the code

as each statement ends with a semicolon.

It is required more for multiple conditional statements.
×

Success!

Help @tree123 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.27,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...