Use indexOf to check if string begins with another string - Javascript jQuery

Javascript examples for jQuery:String

Description

Use indexOf to check if string begins with another string

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript" src="https://code.jquery.com/jquery-1.7.1.js"></script> 
      <script type="text/javascript">
    $(function(){//  w w w.  j av  a2s .  c o m
var foo = "asomething-something";
if(foo.indexOf("something") === 0) {
    console.log("works!");
}
else{
    console.log("doesn't work :(");
}
    });

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

Related Tutorials