JavaScript OR Operator issue

JavaScript OR Operator issue
0

Hello Developers, I am trying to validate my image file format using the below code.

 fileName(e) {
      let selected = e.target.files;
      for (let i = 0; i < selected.length; i++) {
          if(selected[0]['type']!=='image/jpeg' || selected[0]['type']!=='image/png')
          {
              alert("Invalid File");
              return false;
          }
          //console.log(selected[0]['type'])
          this.file_names.push(e.target.files[i]);
          this.file_name_deails.push(e.target.files[i].name);
        //console.log(e.target.files[i].name);
      }

But if I use following code only for jpeg it works

    if(selected[0]['type']!=='image/jpeg' )
          {
              alert("Invalid File");
              return false;
          }

If I use || condition like below it doesn’t work. only show alert and return false.

 if(selected[0]['type']!=='image/jpeg' || selected[0]['type']!=='image/png')
          {
              alert("Invalid File");
              return false;
          }

what is the issue here ? Thank you in advanced

You may want to review the following curriculum challenge:

https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/comparisons-with-the-logical-or-operator

You are not understanding a key concept in how the overall if statement condition will evaluate.

selected[0][‘type’] cannot be jpeg and png at the same time so your if statement will always be true.