-1

I have an object, with some properties in it, and as I'm trying to compare a given string property of this object, waiting for the compared result is true I got false !

Adding an empty string to the property (create a new String) and give the result I was waiting for.

Cann somebody explain me why can't I compare Strings-properties of Objects directly without using a "modified-copy" of it?

(code verified on jsbin.com : https://jsbin.com/kojehinexe/edit?js,console)

var hohoho = {  "testCallback_abc": {
        "abc": {
            "addToNumber": {
                "executed": true,
                "returnedExecutionValue": [42]
            },
            "addToArray": {
                "executed": true
            },
            "addToObject": {
                "executed": true
            },
            "returnATestValue": {
                "executed": true,
                "returnedExecutionValue": ["testValue"]
            }
        }
    }
}

var testString = "testValue";

console.log(hohoho.testCallback_abc.abc.returnATestValue.returnedExecutionValue === testString); // return false
console.log(hohoho.testCallback_abc.abc.returnATestValue.returnedExecutionValue+"" === testString); // return true
Teemu
  • 22,918
  • 7
  • 53
  • 106
mars
  • 1
  • 1
  • 4
    `.returnedExecutionValue` evaluates to an array, which is definitely not `===` to a string. If you coerce it to a string first, it *may* be `===` to some other string. – CertainPerformance Jan 24 '19 at 10:22
  • Possible duplicate of [Difference between == and === in JavaScript](https://stackoverflow.com/questions/523643/difference-between-and-in-javascript) – Cid Jan 24 '19 at 10:24
  • Strict comparison operator (`===`) doesn't cast types, loose comparison operator (`==`) will give you the expected result. – Teemu Jan 24 '19 at 10:24
  • Such a beginner error! I was searching it for an our before decide to post it here ... of course, I'm working with an array ... Sorry ! – mars Jan 24 '19 at 10:26
  • @mars no need to worry, you've learn something, which is nice – Cid Jan 24 '19 at 11:58
  • 1
    More lessons, here at SO we don't stamp the title with "Solved", rather tick a checkmark on the left side of the answer which solved your problem. That way the question becomes properly marked as answered, and you'll gain some rep too. – Teemu Jan 24 '19 at 14:54

2 Answers2

0

The type of returnedExecutionValue is an array and not a string.

you can use typeof to check the type of any variable.

for example:

let x = []
typeof x // "object"

typeof (x+"")  // "string"

This is exactly what you are doing.

In your case, you could try it as

console.log(hohoho.testCallback_abc.abc.returnATestValue.returnedExecutionValue[0] === testString); 
Ashwin Valento
  • 878
  • 1
  • 7
  • 14
0

console.log(hohoho.testCallback_abc.abc.returnATestValue.returnedExecutionValue[0] === testString); // true (as both value and type are same)

OR

console.log(hohoho.testCallback_abc.abc.returnATestValue.returnedExecutionValue == testString); // true (as returnedExecutionValue is an array)