When writing a script that requires handling a pop-up window, you will need to first declare a variable that returns the window handles that you can use to iterate over all open windows. You can do this by using the .getWindowHandles() method. See examples below :
var allWindowNames = driver.getWindowHandles();
Once you've declared this variable you can then pass all of the windows into an array by using the .toArray() method.
var windows = allWindowNames.toArray();
Now you will be able to switch between the windows by using the driver.switchTo() method. When switching to the pop-up window you'll likely need to use the second index in the array "1". If needed you can also switch back to the default window by using the first index in the array "0" or you can use the .defaultContent() method.
// Switch to pop-up window
driver.switchTo().window(windows[1]);
// Switch back to default window
driver.switchTo().window(windows[0]);
// OR
driver.switchTo().defaultContent();
Depending on the application, you might also need to add a delay in the script which waits for the pop-up to display. Normally adding a waitForNetworkTrafficToStop(); method just before the .getWindowHandles() method will allow the script to wait long enough to collect all of the windows.