0

In this example:

someVariable;

does the code do anything? More technically, is there some work associated with it from the point of view of a JS engine like V8?

I'm asking because I'm looking to temporarily suppress the "variable is declared but its value is never read" warning by TypeScript and I'm doing this:

function xyz(arg) {
  arg;
  // ...
}

Is there a better "no-op" construct in JavaScript?

Borek Bernard
  • 50,745
  • 59
  • 165
  • 240
  • 2
    Why not [setup lint accordingly](https://stackoverflow.com/questions/50011443/tslint-how-to-disable-error-somevariable-is-declared-but-its-value-is-never-rea) instead? –  Sep 12 '19 at 12:10
  • I'm pretty sure it does nothing. Well, technically it reads the value but that shouldn't cause any side effects - at most it reads `arg` returns the result and discards it. But I think the execution environment will just optimise it by removing that command altogether. – VLAZ Sep 12 '19 at 12:10
  • 3
    out of curiosity- why do you care about this warning so much? and if you do, why declare a variable you do not use at first place? – Gibor Sep 12 '19 at 12:10
  • is this warning by typescript or by linter ? also if you don't want to use `arg` in function, why not have it `function someName()` – Code Maniac Sep 12 '19 at 12:11
  • paste your code in [AST explorer](https://astexplorer.net/), you will see that there is work required. As looking at the AST ```arg``` is marked as 'ExpressionStatement' with a start and a end as well as a name, – Dan Starns Sep 12 '19 at 12:12
  • @Gibor in fairness, I've had a similar situation - say you create an empty function that is going to serve as default and it's no-op `callback = function(name, age) {}`. However, it's useful to see what arguments it expects, so if you override it, you don't need to find code that calls it.. – VLAZ Sep 12 '19 at 12:12
  • To add to @ChrisG's comment, look [at this answer specifically](https://stackoverflow.com/a/52144874/215552) -- you can disable the linter per-line so you still get the checking for the rest of your code. – Heretic Monkey Sep 12 '19 at 12:17
  • 1
    @VLAZ oh I see what you mean. Technically, you can also write it like this: `callback = function( /* name, age */) {};` if you really care about the warning and still want to have your default with the original parameters as a reference. Even if you call the default with parameters, JS would just throw them away. – Gibor Sep 12 '19 at 12:19
  • I don't have the linter set up, but have you tried `function xyz(_arg) {}` instead? edit: like [this](https://stackoverflow.com/a/52085248/2887218)? – jcalz Sep 12 '19 at 13:36

2 Answers2

1

One thing it does is it checks to see if the variable is defined, If it isn't, it'll throw an error.

If you're worried about side-effects, if you happen to be inside a with statement, it can invoke a getter and run code, but this is unlikely. If the variable name in question isn't local and happens to be a getter on window, it can also run code, eg

Object.defineProperty(window, 'foo', { get() {
  console.log('getting');
}});


console.log('start');
foo;

But this, too, is pretty unlikely.

If you're sure the variable referred to is a normal variable in scope, it won't do anything - it'll just be an unused expression.

CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
1

Beside the checking of the exisiting of the variable, it perfoms an evaluation of the expression.

var foo = {
        get bar() { 
            console.log('get bar');
            return 42;
        }
    };

foo.bar;
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392