Check whether Javascript pop up displayed with Webdriver

public boolean isAlertPresent()
{
    try    {
        driver.switchTo().alert();
        return true;
    }   // try    catch (NoAlertPresentException Ex)
    {
        return false;
    }   // catch}
 
 
if(isAlertPresent()){
  driver.switchTo().alert().accept();
}