Better use

if( ( key = key.trim() ) !== '' ) {

to avoid trimming all keys twice