Five typical javascript interview questions

Source: Internet
Author: User
In the IT field, a large number of javascript developers are required. If this role can best demonstrate your abilities, you have many opportunities to change your company and increase your salary. But before you are admitted to the company, you need to demonstrate your technology ,...

In the IT field, a large number of javascript developers are required. If this role can best demonstrate your abilities, you have many opportunities to change your company and increase your salary. However, before you are admitted to the company, you need to present your skills to pass the interview. In this article, I will show you five typical problems.

Question 1: Scope)

Think about the following code:

(function() {   var a = b = 5;})();console.log(b);

What will be printed on the console?


The above code will print 5.

The trap of this problem is that in the immediate execution of the function expression (IIFE), there are two names, but the variables are declared through the keyword var. This means that a is the local variable of this function. In contrast, B belongs to the global variable of this function.

Another trap of this problem is that it is not used in the function."Strict mode"('Use strict ';). If the strict mode is enabled, the code will report an uncaptured reference error (Uncaught ReferenceError): B is not defined. Remember the strict mode requirement. If this is the expected behavior, you need to explicitly reference global variables ,. Therefore, you need to write as follows:

(function() {   'use strict';   var a = window.b = 5;})();console.log(b);
Question 2: Create a native Method

Define a repeatify function on the String object. This function accepts an integer parameter to specify how many times the string needs to be repeated. This function requires the specified number of times of repeated strings. For example:


Hellohellohello should be printed.


One possible implementation is as follows:

String.prototype.repeatify = String.prototype.repeatify || function(times) {   var str = '';   for (var i = 0; i < times; i++) {      str += this;   }   return str;};

This problem tests developers' understanding of the inheritance in javascript and the prototype attribute. This also verifies the ability of developers to extend native data-type functions (although this should not be done ).

Here, another key point is to demonstrate how you realize that functions that may have been defined are not overwritten. Before using a UDF, you must determine whether the UDF does not exist.

`String.prototype.repeatify = String.prototype.repeatify || function(times) {/* code here */};``

This technique is useful when you are asked for a gasket javascript function.

Question 3: Hoisting)

What are the results of executing the following code? Why?

function test() {   console.log(a);   console.log(foo());   var a = 1;   function foo() {      return 2;   }}test();

The execution result of this Code is undefined and 2.

The reason for this result is that both variables and functions are upgraded (hoisted ). Therefore, at that time, a was printed, and it exists in the function (that is, declared), but it is still undefined. In other words, the above Code is the same as the following code.

function test() {   var a;   function foo() {      return 2;   }   console.log(a);   console.log(foo());   a = 1;}test();
Question 4: How does this work in javascript?

What is the result of the following code? Please explain your answer.

var fullname = 'John Doe';var obj = {   fullname: 'Colin Ihrig',   prop: {      fullname: 'Aurelio De Rosa',      getFullname: function() {         return this.fullname;      }   }};console.log(obj.prop.getFullname());var test = obj.prop.getFullname;console.log(test());

The Code prints Aurelio De Rosa and John Doe. The reason is that in javascript, the context of a function, that is, the keyword "this", references how the function is called, not how it is defined.

In the first console. log () call, getFullname () is called as the obj. prop function. Therefore, the context points to the latter and the function returns the fullname attribute of the object. Conversely, when getFullname () is specified as the test variable, the context points to the Global Object (window ). Because test is equivalent to setting it as a Global Object attribute. For this reason, the function returns a fullname attribute of window, which is set in the first line of the code snippet in this case.

Question 5: call () and apply ()

Fix the previous issue and print Aurelio De Rosa from the last console. log.


You can use the call () or apply () function to force the context of the function. If you do not know the difference between call () and apply (), I recommend that you read the article function. call and function. apply. Is there a difference between them? . In the following code, I will use call (), but in this case, apply () can also get the same result:


In this article, we discuss five typical questions that will be asked during the interview to test javascript developers. The actual questions from the interview may be different, but the concepts and themes covered are usually very similar. I hope you can test your abilities with pleasure. In case you don't know all the answers, don't worry: problems that cannot be solved without learning and experience.

If you are asked other interesting questions during the interview, don't hesitate to share them with us immediately. This will help many developers.

The above are the details of the five typical javascript interview questions. For more information, see other related articles in the first PHP community!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.