-1

The following code snippet should display an alert if x variable have text "I have right text right here" but my code does not work. Why?

var x = "Im the variable and I have right text right here";

if ("x:contains("I have right text right here").lenght > 0") {
  alert("Match")
}
Ram
  • 143,282
  • 16
  • 168
  • 197
Karolina Ticha
  • 531
  • 4
  • 19

2 Answers2

1

The indexOf() method returns the position of the first occurrence of a specified value in a string.

var x = "Im the variable and I have right text right here";

if ( x.indexOf("I have right text right here")  > 0 ) {
  alert("Match")
}
ashkufaraz
  • 5,179
  • 6
  • 51
  • 82
1

You may use includes:

    var x = "Im the variable and I have right text right here";

    if ( x.includes("I have right text right here")) {
        alert("Match")
    }
gaetanoM
  • 41,594
  • 6
  • 42
  • 61