I need to check if a string is equal to a defined value AND if the object has the hash
key.
I am very confused with this:
var my_string = 'some_string';
var my_obj = {'hash':'4010f89a05c236cd4f3a5c7558af0e70dc4216454d2d5155a285bfbad752ce51f9510272821a254'}
console.log((my_string == 'some_string' && my_obj['hash']));
That return 4010f89a05c236cd4f3a5c7558af0e70dc4216454d2d5155a285bfbad752ce51f9510272821a254
Expected true
or false
(in this example expected true
).
I need to check if a string is equal to a defined value AND if the object has the hash
key.
I am very confused with this:
var my_string = 'some_string';
var my_obj = {'hash':'4010f89a05c236cd4f3a5c7558af0e70dc4216454d2d5155a285bfbad752ce51f9510272821a254'}
console.log((my_string == 'some_string' && my_obj['hash']));
That return 4010f89a05c236cd4f3a5c7558af0e70dc4216454d2d5155a285bfbad752ce51f9510272821a254
Expected true
or false
(in this example expected true
).
-
3
The JavaScript operators
&&
and||
do not necessarily yield a boolean value as a result. – Pointy Commented Mar 29, 2018 at 23:22 - Bfff ... I will need to check my entire project :( ... how you suggest to solve this ? (I need to use many times in mi project conditions like this) – MTK Commented Mar 29, 2018 at 23:25
- 3 The operators return the actual values from either the left-hand or right-hand side of the operation. They perform the test by coercing the values to boolean, but they return the actual values. – Pointy Commented Mar 29, 2018 at 23:29
5 Answers
Reset to default 8It's working correctly.
(my_string == 'some_string' && my_obj['hash'])
is equal to "4010f89a05c236cd4f3a5c755..."
which is truthy. This is just fine to use as a conditional in an if
statement for instance.
You can convert it to an actual boolean too:
!!(my_string == 'some_string' && my_obj['hash'])
The && operator returns whatever is on the right side of the && whenever both values are true like this:
const foo = 'foo';
const bar = 'bar';
const foobar = foo && bar;
console.log(foobar);
This returned result is then in turn coerced into a true of false as the result of the if statement. It is important to realise that the if statement coerces the value into a boolean and the && statement does not.
One option is to use the in
operator to check to see if a particular key exists in the object. I prefer this method because JavaScript has some really really awful values that are considered falsey, like 0
.
console.log((my_string == 'some_string' && ('hash' in my_obj)));
&& does not return everytime boolean.
When you use && with string it returns the second value when both are true. When you use || with string it returns the first value when both are true.
let one = "Cat" && "Dog"
let zwo = "Cat" || "Apple"
one returns Dog. two returns Cat
You can use a ternary operation and make it return true or false.
Like so:
console.log( (my_string == 'some_string' && my_obj['hash']) ? true : false );
More info here:
- https://www.w3schools./jsref/jsref_operators.asp (search for "Conditional (Ternary) Operator")
- https://learn.microsoft./en-us/scripting/javascript/reference/conditional-ternary-operator-decrement-javascript