There are couple options and interviewer looking on multiple things in your answer.

Sample Answer:

JQuery provides string trimming function and if our project already uses JQuery we can use jQuery.trim(string) function. If not, the simples way would be to utlize regular expressions and extend string object by using prototypes as following:

String.prototype.trim = function() {
return this.replace(/^s+|s+$/g,"");

It worth mentioning that JS regular expressions are compatible with Javascript 1.2+ and all modern browsers will support this.

Answers and Comments