-3

My code includes the following-

 if(!code1.startsWith(("<video" || "<img"))){return ...}

The code only checks if code1 doesn't start with "<video". If the "<video" is false and "<img" is true, it executes the return code. I am still a learner and hoping for help.

Arusu
  • 3
  • 4
  • [The docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith) says about the parameter: "_All values ... are coerced to strings_". The argument itself is evaluated before calling the method, that's why "video" is always passed. – Teemu Mar 29 '23 at 11:23
  • 2
    You need two tests. `if(!code1.startsWith(" – mplungjan Mar 29 '23 at 11:23

1 Answers1

1

it is not supposed to behave like that; but you can achieve it this way:

if (!(code1.startsWith("<video") || code1.startsWith("<img"))) {
    return ...
}

The code above checks that code string NEITHER starts with <video NOR <img - so if it starts with either, return ...

mplungjan
  • 169,008
  • 28
  • 173
  • 236
Mechanic
  • 5,015
  • 4
  • 15
  • 38
  • 1
    This worked! I am sorry I misunderstood how it worked. I am validating an input field if what I set was edited (node.js). Thank you. – Arusu Mar 29 '23 at 11:41