Wednesday, March 6, 2013

Sample Javascript Interview Questions


//1
//what value will show with the alert?
test = "herby";
{
    var test = "not herby";
}
alert(test);
//1

//2
//what value will show with the alerts?
//how would you write this differently
function triple(x)
{
i= 3;
return x * i;
}
var i =2;

alert(triple(i)) ;
alert(i);
//2

//3
//will this run? why or why not?
var o = new Object;
o.totriple = triple;
with(o)
{
alert(totriple(5));
}
//

//4
//what are the results and why
alert("1"==true);
alert("1"===true);
//

//5
//what value will show with the alert?
//this applies to other languages also
var x = 1;
var y = x++;
alert(y);
//5

//6
//will the alert run?
//bonus - what is this called?
if(x==0 && y ==0);
alert("can this be?");
//

//7
//what are the results here?
alert("1" + 2);
alert(1 + "2");
alert(1 + 2 + " hello");
alert(" hello" + 1 + 2);
alert("19" < "2");
//7

//8
//question - can you make javascript mimic c#'s startwith function for strings?
String.prototype.startsWith = function(s){ return (this.substr(0, s.length) == s)  }
var test ="test";
alert(test.startsWith("te"));
alert(test.startsWith("ete")); 
//8

//9
//what value is num?  
var num = 34/0;
alert(num);
//9

//10
//will the value be true and why?
//is there any way of doing this?
var num2 = 0/0;
var num3 = 0/0;
alert(num2==num3);
//10

//11
//what will show?
alert("n\n\\n");
//11

//12
//is the if statement true
var num4;
if(num4===null)
  alert("here");
//12

//13
var t   = 0||67;alert(t); 
//13


No comments:

Post a Comment