Elias Sjögreen Elias Sjögreen - 4 months ago 50
Node.js Question

Check if string contains word at start of string

I am starting to learn a little bit more advanced js and i want to check if string contains a word at the start of the line.
Something like this would be good!:

var string = "Hello world";
if (string.contains("Hello", start) == true)
{
console.log("success");
}

Answer

Very easy Method:

var str = "Hello World";

if (str.startsWith("Hello")){
    do stuff;
}

You could also make two arrays and compare the single letters using a for loop, slightly more creative :D