3

This is a two part question. And is different than other questions that are based on equality in JavaScript because it deals with a specific warning that is displayed in the IDE "Visual Studio Code" and why that warning message is or is not in error.

I'm getting the following warning message in Visual Studio code:

Operator '===' cannot be applied to types 'number' and => 'string'

on the following line of code:

var x = 23;
var y = "23";

x.valueOf() === y.valueOf() ? 
    console.log("Types ARE the same") 
        : console.log("Types ARE NOT the same");

Now this technique definitely works for me and I understand the reason for using a triple equal for this type of thing rather than a double equal. This same code I have learned in several fundamentals courses at codeschool and pluralsight as a way to generate a boolean value based on the type of two different values where the product or returned value is that of either true or false and the technique can come in handy.

MY QUESTIONS:

How is the code above any different from:

 x === y ? 
    console.log("Types ARE the same") 
        : console.log("Types ARE NOT the same");

in this simple example of comparing two simple values and their types, is there something additional I can do by using the valueOf() method along with it?

Also is Visual Studio Code wrongly generating this warning and if not is there a better way to do this type of check?

Because no matter what method I use above I get the same warning error in Visual Studio Code, but it works fine for me in practice.

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
Eric Bishard
  • 5,201
  • 7
  • 51
  • 75
  • 2
    The warning is because `(23).valueOf()` is just `23`, and `"23".valueOf()` is just `"23"`. (There’s no point in using `valueOf`.) Since they aren’t the same type, the condition will always be false. If this is your intent – using `===` specifically to compare types – then you can safely ignore the warning. Most of the time, though, you’ll want to compare values of the same type with `===`, and VS is warning you of a potential mistake. – Ry- Sep 10 '15 at 02:06
  • possible duplicate of [equality in javascript](http://stackoverflow.com/questions/9751056/equality-in-javascript) – Jan Sep 10 '15 at 03:23
  • 2
    The `valueOf` doesn't matter, but VS correctly identifies that you're using type sensitive comparison `===` comparing two different types. So your expression will ALWAYS return false. It could just as well just say `false ? ...` or better yet, just remove the entire first part of the statement, and VS recognizes this. Hence the warning, your statement is completely superfluous and could safely be removed. – Jan Sep 10 '15 at 03:25
  • Ahh so if I use it in a function where the parameters type is unknown it shouldn't give me this error right? It's giving me the error because it knows I defined the variables as different types and those values are hardcoded. Right? – Eric Bishard Sep 10 '15 at 03:31
  • Check out my answer jan and see if you agree that this is what the issue was. Thanks for the help! – Eric Bishard Sep 10 '15 at 03:39
  • I definitely understand why you would think it might be a duplicate of "equality in JavaScript" but I think it touches on something a little deeper than the difference between double equal and triple equal as it hits on the context of the use and the specific warning in Visual Studio Code that at first makes you say, wait, that's perfectly legal. So I think this question might serve a purpose above and beyond simply being similar to other "equality JavaScript" questions. At least that's my story and I'm sticking to it. lol But I'm not mad for you pointing that out and it's a valid point. – Eric Bishard Sep 10 '15 at 03:55
  • I provided my own explanation of it, I'll leave it up to you if you think yours explains the matter better though. – Jan Sep 10 '15 at 16:56

3 Answers3

2

Going by what I see from the MDN docs here for String.prototype.valueOf(); and here for Number.prototype.valueOf(); and the code that you have, I must say you already have x and y holding primitive values of string and number. There is no need to do a valueOf() on x and y as they are not of Object type.

You would ideally use String.prototype.valueOf(); on a String Object to get it's primitive value and Number.prototype.valueOf(); to get primitive value of the Number Object which result in string data type and number data type values respectively.

Hence, the warning I suppose. Although I dont have prior experience with visual-studio, but I feel it understands that .valueOf() should be used on an Object type rather than that on a primitive type.

Sudhansu Choudhary
  • 3,322
  • 3
  • 19
  • 30
  • I definitely understand what you are saying, but I do find many reasons why comparing two values like this in code where values may be unknown and the possibility of getting one value that is a string of a number and another value that is actually an integer need to be evaluated and know if they are different or produce a false product from this comparison. In that case I may want to handle the data differently or make conversions from string to numeric. Right? – Eric Bishard Sep 10 '15 at 03:14
  • 1
    I agree with you. I have worked on different IDE(s) and never had to face this.. Even tried this particular code of yours.. Didn't get a warning. A programmer should be allowed to compare values in JS using either `==` or `===` if the operands are comparable, which in your case, they are. So I feel there shouldn't be any such "warnings". Having said that I still feel the second para from my answer could be the reason or it could be that VS knows that the condition **will be false** because it knows you have declared the variables. Have you tried `x === y` without the `valueOf()` stuff? – Sudhansu Choudhary Sep 10 '15 at 03:33
  • Check out the answer I just provided I think we figured it out and Visual Studio Code was actually correct in issuing a warning. – Eric Bishard Sep 10 '15 at 03:40
  • Cool.. great! Good that you answered :) – Sudhansu Choudhary Sep 10 '15 at 03:55
  • 1
    Well, we all answered it, and Jan was helpful in figuring out why Visual Studio Code would warn against it. It's a group effort, That's what I love about Stack Overflow sometimes, because I just got a very in depth understanding of the context of using that statement. – Eric Bishard Sep 10 '15 at 03:57
  • yeah.. I mean, you did the right thing by putting up your understanding in statements and answering here.. that would help many!! +1 to the answer – Sudhansu Choudhary Sep 10 '15 at 04:28
1

So because the example above is comparing two known defined values that are obviously of different types, Visual Studio Code is actually correct in issuing a warning, because it knows that for a fact variable x is a string and variable y is an integer and therefore warns (very vaguely and not that intuitively for me anyways) that by comparing these two known hard coded values we will always get a false, and if we in fact drop one of the equal signs and make it a double equal operator, then it will always equate to true because it will enforce "Type Coercion" which will change the string value to an integer. Therefore if we setup this same type of ternary statement inside of a function where the parameters are possibly unknown and THE WARNING MESSAGE DOES GO AWAY:

function compareTwo(a,b) { 
    a.valueOf() == b.valueOf() 
        ? console.log("value types ARE the same") 
            : console.log("value types ARE NOT the same");
}

Because it's now possible that anyone can pass anything into this function and it no longer will always be true or false in either scenario.

Eric Bishard
  • 5,201
  • 7
  • 51
  • 75
1

The valueOf doesn't matter and has nothing to do with the warning. But VS correctly identifies that you're using type sensitive comparison === comparing two different types. Your expression 23 === "23" will ALWAYS return false, so it could just as well just say

false ? doIfTrue() : doIfFalse();

or even better yet, just remove the entire first part of the statement since it will never fire

doIfFalse();

VS recognizes this, hence the warning. Your conditional statement is completely superfluous and could safely be removed.

You would receive a similar warning if you were to write something like

if (true) doSomething();

or

if (true == false) doSomething();

VS will recognize that the conditional holds no meaning and makes code either fire every time or completely unreachable, and it warns you about it (as it should).

And like you noted: If the variables aren't hard-coded in the scope, the warning will go away, since VS stops validating type of variables set outside the function. Javascript isn't a typed language and keeping track of the content of all variables would just be too taxing and complex for the IDE.

Depending on your coding style, for all VS knows it might even be your desired behaviour (if variable types match, do something, otherwise, do something else). When the variables are set in the same scope though, it just creates unreachable code.

Jan
  • 5,688
  • 3
  • 27
  • 44
  • Hi Jan, I would rather pick any answer aside from my own if I think it addresses the initial question and problem better, in this case, I understand how ternary statements and conditionals work but I was confused as to the exact reason VS Code was giving the error and was looking for a solution using similar code in the question that would make the error go away truly as an exercise of the brain not to just get rid of the error. I wanted to understand it better. In that case I feel that my answer is more to the point and after reading it back days later I still makes me understand better. – Eric Bishard Sep 25 '15 at 04:14