Tim Marshall Tim Marshall - 1 year ago 83
Javascript Question

Expected "{" and instead saw "return"

Learning, cleaning up and updating a snippet I found on-line. I've been adding the somewhat new code

"use strict";
and the way the snippet has been programmed is alike the below snippet;

81| document.addEventListener("mouseup", function() {
82| "use strict";
83| if (!dragging) return;
84| $scrollbar.css('z-index', dragging.z_idx);
85| $scrollbar.removeClass('active-scroll');
86| dragging = null;
87| }, true);

Line 83 is pulling up the following error;

Expected "{" and instead saw "return".

I know how to bracket this section however I'm not sure how the
is doing exactly, whether I should ass the
before the return and then close up after
dragging = null;

This is also used as seen bwloe;

91| dragTarget.addEventListener("mousemove", function(e) {
92| "use strict";
93| if (!dragging) return;
95| $scrollbar.offset({
96| top:limit(e.clientY - dragging.pos_y, dragging.drg_h)
97| });
98| update_scroll();
99| }, true);

Answer Source

Some automated tools consider placing a "naked" statement after a conditional (i.e. a statement not wrapped in a block) as bad practice.

Some even warn about multiple return statements or about writing if (x == 3) ... instead of if (3 == x) ....

The original statement if (!dragging) return; is perfectly valid Javascript. If it's more readable for you then just keep it that way and shut up that warning (there should be options to do that).