📜 ⬆️ ⬇️

Questions at the javascript interview

I offer a selection of questions that you can ask the candidate. Answers are displayed in a pop-up window.
The 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 is not scary if the answer is different from the real one, because there are tricks in the questions. Then you will need to explain how to get such a result.
Copy the code into 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() { } Ca = 2; var g = new C(); alert("Test 4: " + ga); // **************************************************************************** var s = {a: 0}; s.prototype = {b: 1}; alert("Test 5: " + (sa == sb)); // ******************************************************************************* function A() { this.a = 4; } A.prototype = {a: 2}; var x = new A({a: 7}); alert("Test 6.1: " + Aa); alert("Test 6.2: " + xa); // *********************************************************************************** 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}; Ba = 4; var m = new B(3); var j = new B(); alert("Test 10: " + ma + ja); // ****************************************************************************** 


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

Test 2.1: false
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

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

Test 5: false
s.prototype is a red herring. prototype works only on constructors (functions). Therefore, sb is undefined.
And here the hook is - 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. Constructor arguments were ignored.

Test 7: undefined
In the func function we looked, more undefined zero, less or equal. None of the conditions are true. All three if fails. Returns undefined.
This, by the way, is a good trick. I once got into the program.

Test 8: ReferenceError: NAN is not defined
There will be an exception when checking tst> NAN. NAN is an undefined symbol (unlike NaN).
By the way, when comparing tst> window.NAN exceptions would not have happened.

Test 9.1: true
The variable x was created in a global context - i.e. she is a member of window. When you run 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, it could be redefined.

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

Source: https://habr.com/ru/post/253033/


All Articles