12

Consider this JavaScript snippet:

var a = document.createElement("a");
a.style.display = "none";

IntelliJ IDEA 2016.2 highlights a.style.display and gives this hint:

Value assigned to primitive will be lost

Checks for improper usage of wrappers for JavaScript primitive types. Also, warning will be produced when property of primitive type is modified, as assigned value will be lost

IntelliJ IDEA hint

CSSStyleDeclaration is not a primitive type, thus the assignment to display won't be lost.

String is a wrapper type. How am I using it (improperly)?

The code seems to work fine in Chrome and Edge. What's the deal?

7
  • stackoverflow.com/questions/37923424/…
    – Rayon
    Commented Jul 19, 2016 at 16:09
  • I've seen that question but didn't really understand the comments. Also, I don't have a loop or even a function here.
    – xehpuk
    Commented Jul 19, 2016 at 16:22
  • Are you inside a function ?
    – Rayon
    Commented Jul 19, 2016 at 16:23
  • In my original code, yes, I am inside a function. But as you can see from the screenshot, this even happens if the snippet is the sole content of the file.
    – xehpuk
    Commented Jul 19, 2016 at 16:26
  • Issue is listed here
    – Rayon
    Commented Jul 19, 2016 at 16:42

1 Answer 1

1

There's nothing improper in your code, IntelliJ's type inference is getting a bit confused (this aspect of JavaScript is particularly confusing).

Its linter sees a string and assumes you will try something like this:

var primitive = "september";
primitive.vowels = 3;

primitive.vowels;
// => undefined

Which would lead to a 'lost' value.

To further understand this weird part of JavaScript, I recommend Angus Croll's excellent in-depth article here.

Not the answer you're looking for? Browse other questions tagged or ask your own question.