Use string indexOf to check if a substring exists - Javascript String

Javascript examples for String:substring

Description

Use string indexOf to check if a substring exists

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
    window.onload=function(){// w w w  .  j  a  v a 2s . c om
var password = 'try';
var emailSubstring = 'try@email.com';

if ( password.indexOf( emailSubstring ) != -1 ) {
    console.log( '1' );
}
if ( emailSubstring.indexOf( password ) != -1 ) {
    console.log( '2' );
}

    }

      </script> 
   </head> 
   <body>  
   </body>
</html>

Related Tutorials