最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript and (&&) operator not working as expected - Stack Overflow

programmeradmin8浏览0评论

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).

Share Improve this question edited Mar 29, 2018 at 23:45 Adriano 3,9345 gold badges35 silver badges55 bronze badges asked Mar 29, 2018 at 23:20 MTKMTK 3,5705 gold badges38 silver badges59 bronze badges 3
  • 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
Add a ment  | 

5 Answers 5

Reset to default 8

It'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
发布评论

评论列表(0)

  1. 暂无评论