Why does my if statement work with an else if, but not an OR operator

why is my else statement not working java
why is my else statement not working python
if else statement
if else statement example
if else java
nested if-else flowchart
how do if statements work
if condition

I wrote a small piece of JS in the console, to loop through recommended connections on LinkedIn and if the text contains a certain word, ignore that card, otherwise click the 'X' close button.

Initially I wrote it like this:

const list = document.querySelector('.mn-pymk-list__cards');
const cards = list.querySelectorAll('.mn-pymk-list__card');

cards.forEach( (card, i) => {

  setTimeout( ()=>{
    let text = card.querySelector('.member-insights__count');

    if( !text.textContent.includes('Sharon') || text === null ) {
        card.querySelector('.pymk-card__close-btn').click();
       } else {
        card.style.background = 'green';
       }
  }, i * 1000 )

});

However, when it ran, it would sometimes error (whilst continuing to iterate) with 'Could not read textContent of null'.

However, when I wrote the code like this:

const list = document.querySelector('.mn-pymk-list__cards');
const cards = list.querySelectorAll('.mn-pymk-list__card');

cards.forEach( (card, i) => {

  setTimeout( ()=>{
    let text = card.querySelector('.member-insights__count');

    if( text === null ) {
        card.querySelector('.pymk-card__close-btn').click();
    } else if (!text.textContent.includes('Sharon')) {
        card.querySelector('.pymk-card__close-btn').click();
    } else {
        card.style.background = 'green';
    }
  }, i * 1000 )

});

It runs absolutely fine and does what I want it to.

QUESTION: I can't understand why the first option doesn't work, as it seems more concise and theoretically should do the same thing?

I suspect it has something to do with the fact that on LinkedIn, some of the suggested contacts don't have a class of '.member-insights__count' and instead have '.member-insights__info'.

But, that should still make text resolve to null, right?

Any insight would be great!

if( !text.textContent.includes('Sharon') || text === null ) {

What would you expect this code to do when text is null? It will crash because you can't access properties or methods of null.

You need to check for null first.

if( text === null || !text.textContent.includes('Sharon') ) {

Not Equal To Operator in Excel, How do you write not equal to in an if statement? That trailing ; is making Java believe that the if statement is finished, and the {} block after it is outside the if condition, consequently the else part has no matching if preceding it. This is a rather frequent bug, and a hard one to spot. If it weren't for the else block, the

If textContent is null (e.g. as you said, the contact has '.member-insights__info' instead), !text.textContent.includes('Sharon') will cause the error message.

In the second version, that line can only be reached if the text isn't null.

can you have two conditions in an if statement, to each other. Let's take a look at a few examples. Testing a condition is inevitable in programming. We will often face situations where we need to test conditions (whether it is true or false) to control the flow of program.

The || operator will only evaluate the 2nd argument if the first argument is false.

In your case the only difference between the first and second code snippets is the order in which you check if the value is null

What you need to do is check for null first like this:

if( text === null ||  !text.textContent.includes('Sharon')) {

This way it will only check if text includes specific text once it's determined that text is not null

We can do things conditionally in our programs using if statements and if/else the equality operator will just read the value of the variable and see if it's else { println('You have no voice in government! Test your code by changing variables until you make it inside each of the blocks of code, so that you know it all works. I don't understand how if I'm getting only the two numbers I'm checking for some of them aren't being caught by my if statements. To complete the assignment I've changed the second if statement into an else statement and everything seems peachy, but I'd really like to understand what's going on here.

if (choice == 'A'){ if (hours <= 10) { System.out.println ("Your total charges are: " + chargesa);} else if (choice == 'A') if (hours > 10){  [code]if ( a ) { } else if ( b ) { } [/code]Is structurally different from: [code]if ( a ) { } else { } if ( b ) { } else { } [/code]And: [code]if ( a) { /*…*/ } if

With the if/else statement, the program will execute either the true code block a single statement for the else condition, you do not need to use curly brackets. if  You can use an "else if" statement following an if statement and its body; that way, if the first statement is true, the "else if" will be ignored, but if the if statement is false, it will then check the condition for the else if statement. If the if statement was true the else statement will not be checked. It is possible to use numerous else

To do that, we can use the if statement and the conditional operator ? , that's also called a The if statement may contain an optional “else” block. It's not recommended to use the question mark operator in this way. Excel IF Statement – How to Use IF Statement is one of the most popular instructions among the Decision Making statements. IF Statement gives the desired intelligence to a program, so that it can take decisions based on a criteria and most importantly decide the program flow.

Comments
  • Your error suggests that the element you're trying to grab with let text = card.querySelector('.member-insights__count'); isn't grabbed and thus textContent can't be read on the element stored in text because there is nothing stored in text
  • "I suspect it has something to do with the fact that on LinkedIn, some of the suggested contacts don't have a class of '.member-insights__count' and instead have '.member-insights__info'." it sounds like you already solved your question. If no matches are found with querySelector, it will return null. OH i see yes you need to swap your || conditions, text == null will short circuit the or statement and not try to evaluate the second part.
  • Simply because if text is null it won't be able to access textContent. If you switch the order of the operands of the || you could achieve the same effect as with the else if statement (it won't evaluate the rhs if the lhs is truthy)
  • So in IF statements using OR operators, you can't simply put to things that are evaluated at the same time? I was assuming you see that text would simply equal null and therefore whilst the left hand side of the IF does nothing (or errors) the right hand side would sort it out... I didn't know an IF could crash so to speak :) Thanks for the answer.
  • @WillOSW: They don't get evaluated at the same time, no. They get evaluated in the order they appear. And in the case of JavaScript, once the overall condition is true, then no more conditions are tested. By switching them around, as I have it, the textContent property is never evaluated when text is null. And so you don't get the error.
  • "They get evaluated in the order they appear" Worth mentioning Javascript's operator precedence for reference.
  • Right ok, but if that part of the IF statement causes an error, won't it still run because the other side of the or will be equal to null text === null ?
  • It doesn't check them at the same time, it does one after the other. So first it will check !text.textContent.includes('Sharon'), THEN text === null. so it hits the first part and fails. As others have said, if you check for null first, it will only check !text.textContent.includes('Sharon') if the text is not null.