The demand for JavaScript programmers in the IT world is huge. If you are very proficient in JavaScript, you will have a lot of opportunities to change jobs and raise salaries. But before a company hires you, you have to pass the interview and prove your skills. In this article, I'll show you 5 JavaScript-related questions that will give you a comprehensive test of the interviewer's JavaScript skills and the ability to solve problems. Take a look at these 5 classic JavaScript interview questions.
Problem 1:scope range of action
Consider the following JavaScript code:
(function() { var a = b = 5;}) (); Console.log (b);
What kind of results will be output?
Reply
The above code will print 5.
The trick to this question is that there are two variable declarations, but a is declared with the keyword var. Represents a local variable of a function. In contrast, B becomes a global variable.
Another trick of this question is that it does not use strict mode (' using strict ';). If strict mode is enabled, the code throws a Referenceerror error: B is undefined (b is not defined). Keep in mind that strict mode requires explicit designation in order to implement a global variable declaration. For example, you should write:
(function() { ' use strict '; var a = window.b = 5;}) (); Console.log (b);
Issue 2: Create a "native" (native) method
Defines a repeatify function for a string object. When an integer n is passed in, it returns the result of repeating the n-th string. For example:
Console.log (' Hello '. repeatify (3));
The Hellohellohello should be printed.
Reply
One possible implementation is as follows:
function (times) { var str = ' ; for (var i = 0; I < times; i++) { this; } return str;};
This issue tests the developer's knowledge of JavaScript inheritance and prototype. This also verifies whether the developer knows if the built-in object is extended (although this should not be done).
Another important point here is that you need to know how to not overwrite features that might already be defined. Do not exist until you test the function definition:
function (Times) {/**/};
This technique is especially useful when you are asked to do JavaScript functions that are compatible.
Issue 3: Claim elevation (hoisting)
Execute this code and output what results.
function Test () { console.log (a); Console.log (foo ()); var a = 1; function foo () { return 2; }} Test ();
Reply
The result of this code is undefined and 2.
The reason is that the declarations of variables and functions are advanced (moved to the top of the function), but the variables do not have any values assigned. Therefore, when the variable is printed, it exists in the function (it is declared), but it is still undefined. In other words, the above code is equivalent to the following:
function Test () { var A; function foo () { return 2; } Console.log (a); Console.log (foo ()); = 1;} Test ();
Question 4:this How it works in JavaScript
What result does the following code output? Give your answer.
var fullname = ' John Doe '; var obj = { ' Colin ihrig ', prop: { ' Aurelio De Rosa ', function() { returnthis. FullName;}} ; Console.log (Obj.prop.getFullname ()); var test = obj.prop.getfullname;console.log (test ());
Reply
The answer is Aurelio De Rosa and John Doe. The reason is that in a function, the behavior of this depends on how the JavaScript function is called and how it is defined, not just how it is defined.
In the first console.log () call, Getfullname () is called as a function of the Obj.prop object. So, the context refers to the latter, and the function returns the FullName of the object. Conversely, when Getfullname () is assigned to the test variable, the context refers to the Global Object (window). This is because test is a property that is implicitly set to the global object. For this reason, the function returns the FullName of the window, that is, the value defined in the first row.
Question 5:call () and apply ()
Now let you solve the previous problem and make the final Console.log () print Aurelio De Rosa.
Reply
The problem can change the function context by forcing the use of call () or apply (). I will use Call () below, but in this case, apply () will output the same result:
Console.log (Test.call (Obj.prop));
Conclusion
In this article, we've discussed the five classic questions that are used to test a JavaScript developer. The concept of interviewing and the topics covered are usually very similar. If you don't know the answer to some of the questions, don't worry: learning and experience can accumulate slowly.
If you have some other interesting questions, don't hesitate to share them with us. It will help many developers.
5 Classic JavaScript Interview Basics questions