7 JavaScript interview questions for true and false javascript questions

Source: Internet
Author: User
Tags javascript array hasownproperty

7 JavaScript interview questions for true and false javascript questions

The following seven JavaScript interview questions should be asked before the interview. Otherwise, your time may be wasted.

1. What are the two methods for creating JavaScript objects?

This is a very simple problem if you have used JavaScript. You have at least learned one way. However, based on my experience, many people who claim to be JavaScript programmers say they do not know how to answer this question.

  • Use the "new" keyword to call a function.
  • Open/close curly braces.
var o = {};

You can also ask, "When to create an object using the new keyword ?" However, I only want to eliminate some people, so I will wait for the real interview to ask these questions.

2. How to create an array?

This is the same level as "how to create objects. However, some people cannot answer the first question.

Use the following code to create an array:

var myArray = new Array();

Creating arrays is a complicated process. But I want to hear the answer using square brackets from the applicant's mouth.

var myArray = [];

Of course, we can continue to ask other questions, such as how to efficiently Delete repeated elements in the JavaScript array. However, as we only need to know whether the candidates are worth further observation, I will end the problem of array.

3. What is Variable Hoisting )?

This question is a little difficult, and I do not require the other party to answer it. However, through this question, we can quickly determine the technical level of candidates: Do they really understand the programming language as they declared?

Variable escalation means that no matter where the variable is declared within a range, the JavaScript engine moves the declaration to the top of the range. If a variable is declared in the middle of the function, for example, assigning a variable to a row:

Function foo () {// code var a = "abc" is omitted here ";}

The code will actually run like this:

Function foo () {var a; // some code a = "abc";} is omitted here ";}
4. What are the risks of global variables and how to protect the code from interference?

The danger of global variables is that others can create variables with the same name and overwrite the variables you are using. This is a headache in any language.

There are also many preventive methods. The most common method is to create a global variable that contains all other variables:

var applicationName = {};

Then, whenever you need to create a global variable, attach it to the object.

applicationName.myVariable = "abc";

Another way is to encapsulate all the code into an automatically executed function, so that all declared variables are declared within the scope of the function.

(function(){   var a = "abc";})();

In reality, you may use both methods.

5. How to iterate through member variables in JavaScript objects?
for(var prop in obj){    // bonus points for hasOwnProperty    if(obj.hasOwnProperty(prop)){        // do something here    }}
6. What is Closure )?

A closure allows a function to be defined within the scope of another external function. Even if everything in the scope disappears, it can still access the variables in the external function. If the applicant can explain the risks of using closures in the for/next loop without declaring variables to save the current value of the iteration variable, the other party should be given extra points.

7. Describe the JavaScript unit test you have experienced.

For this question, we just want to see if the applicant has actually performed a JavaScript unit test. This is an open question and there is no specific correct answer, but the other party must at least be able to tell something in the process.


Reference: http://www.codeceo.com/article/7-javascript-interview-qa.html

Copyright statement: I feel very grateful if I still write well, I hope you can use your mouse and keyboard to give me a thumbs up or give me a comment! _______________________________________________________________ You are welcome to reprint. I hope to add the original address while you reprint it. Thank you for your cooperation.

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: info-contact@alibabacloud.com 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.