Search code examples
javascriptstringstartswith

Check if a String startsWith() and isn't equal to another String


I wanna know if a String (for example: "Hello World!") starts with "Hello" and isn't equal to "Hello Wourld" but I don't know how to make it. I tried: if(String.startsWith("Hello") && (String !== "Hello Wourld")) { // Do something } But it doesn't work. Can someone help me? Thanks


Solution

  • you can use a regular expression to test for "starts with" in any version of javascript.

    var value = 'Hello There';
    
    if (/^Hello/.test(value) && value !== 'Hello World') {
      console.log('yes it is!')
    }