check if string has quotes javascript

    By: Manu
    2 years ago

    Looks like we are checking if sting is like this

    "some string"
    
    or
    
    'some string'
    

    To detect the quotes we can use this code

    let string = "some string";
    
    if(string.indexOf('"') > -1){
     
     // then do something
    
    }
    
    or simply use
    
    if(string[0] === '"' && string[string.length-1] === '"'){
    
     // then do something
    
    }
    

    If you want to find out if string contains quote or not you might like this

    let string = 'Some string " with double quote';
    
    if(string.indexOf('"') > -1){
    
     // Check this and do something.
    
    }
    

    Hope this helps.