Javascript interview questions

I propose a selection of questions that you can ask the candidate. Responses are displayed in a popup window.
The test subject is asked to predict what will appear in the alert window.
The main thing here is to explain why such an answer was received.
It’s not scary if the answer is different from the real one, because there are some subtleties in the questions. Then it will be necessary to explain how such a result turned out.
Copy the code to the browser console or paste it into the html page. After each alert, ask the interviewee what will happen next, and why.

So, let's begin:
alert("Start");
// ***************************************************************************************
alert("Test 1.1: " + typeof Array);
alert("Test 1.2: " + typeof Array.prototype);
alert("Test 1.3: " + typeof Array.prototype["push"]);
// ***************************************************************************************
var n = "";
if (n)
{
    alert("Test 2.1: " + 'true');
}
else
{
    alert("Test 2.1: " + 'false');
}
alert("Test 2.2: " + (null == undefined));
alert("Test 2.3: " + (n == !!n));
// ******************************************************************************
alert("Test 3: " + ({}.a != {}.b));
// ****************************************************************************************
function C()
{
}
C.a = 2;
var g = new C();
alert("Test 4: " + g.a);
// ****************************************************************************
var s = {a: 0};
s.prototype = {b: 1};
alert("Test 5: " + (s.a == s.b));
// *******************************************************************************
function A()
{
    this.a = 4;
}
A.prototype = {a: 2};
var x = new A({a: 7});
alert("Test 6.1: " + A.a);
alert("Test 6.2: " + x.a);
// ***********************************************************************************
var a;
function func(arg)
{
    if (arg < 0)
    {
        return 1;
    }
    else if (arg > 0)
    {
        return 2;
    }
    else if (arg == 0)
    {
        return 3;
    }
}
var b =  func(a);
alert("Test 7: " + b);
// *********************************************************************************
try
{
    var tst = 5 / 0;
    if (tst > NAN)
    {
        throw new Error('bigger');
    }
    else if (isFinite(tst))
    {
        throw 'lower';
    }
    throw 0;
}
catch (e)
{
    alert("Test 8: " + (e.toString()));
}
// ******************************************************************************
var x = undefined;
alert("Test 9.1: " + (this['x'] === x));
alert("Test 9.2: " + (this['x'] === undefined));
alert("Test 9.3: " + this.hasOwnProperty('undefined'));
// ******************************************************************************
function B(arg)
{
    this.a = arg;
}
B.prototype = {a: 1};
B.a = 4;
var m = new B(3);
var j = new B();
alert("Test 10: " + m.a + j.a);
// ******************************************************************************


Update:
Answers to questions + comments:
Look
Test 1.1: function
Array is a function, like all classes in javascript.
Test 1.2: object
function prototype is Object
Test 1.3: function
push is the Array method defined in prototype

Test 2.1: false the
empty string is converted to false
Test 2.2: true
null and undefined when converting to boolean both give false
Test 2.3: true
! ! n = false, n (empty line) is also false

Test 3: false
{} .a - undefined, {} .b - also undefined. They are equal to

Test 4: undefined
Ca = 2 does not create new members in the constructor of C. Therefore, in the object C a is not defined.

Test 5: false
s.prototype is a distraction. prototype only works on constructors (functions). Therefore sb is undefined.
And here's a catch-up - are 0 and undefined equal? No, not equal.

Test 6.1: undefined
Function A does not have property a.
Test 6.2: 4
When we created x, in constructor A, property a became 4. The constructor arguments were ignored.

Test 7: undefined
In the func function we looked at more than undefined zero, less than or equal to. None of the conditions are true. All three if do not work. Will return undefined.
This, by the way, is a good catch. Once I got into the program.

Test 8: ReferenceError: NAN is not defined
An exception will be thrown here when checking tst> NAN. NAN is an undefined character (unlike NaN).
By the way, when comparing tst> window.NAN, an exception would not have occurred.

Test 9.1: true The
variable x was created in a global context - i.e. she is a member of window. When running the function this = window. That is, this condition is true.
Test 9.2: true
Yes, window.x is undefined.
Test 9.3: true
Here is an interesting point. What is undefined really? This is the variable window.undefined. In earlier versions of the browser, you could override it.

Test 10: 3undefined The
variable m is created as an instance of class B - the number 3 is passed to the constructor. Property a of m is set to 3 in the constructor.
For j, arg = undefined is passed to the constructor. Accordingly, the property a is the same.


Also popular now: