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, suchHow to efficiently Delete repeated elements in the JavaScript ArrayBut since we only need to know whether the candidates are worth further observation, I will end up with questions about arrays.

ForHow to efficiently Delete repeated elements in the JavaScript ArrayLet's talk about: How js implements array reorganization to find 5 methods.

Specific methods:

1). traverse the Array Method

The simplest de-duplication method is to create a new array and traverse the input array. If the value is not in the new array, add it to the new array. Note: the method "indexOf" for determining whether the value is in the array is ECMAScript5, which is not supported by IE8 or earlier. You need to write more compatible low-version browser code. The source code is as follows:

// Function unique1 (array) {var n = []; // a new temporary array // traverses the current array for (var I = 0; I <array. length; I ++) {// if the I of the current array has been saved in a temporary array, skip this step. // otherwise, push the current item to the temporary array if (n. indexOf (array [I]) =-1) n. push (array [I]);} return n;} // determines whether the browser supports indexOf. indexOf is less than IE8, the new method of ecmaScript5 (IE8 and IE8 only support part of ecma5) if (! Array. prototype. indexOf) {// added the indexOf method Array. prototype. indexOf = function (item) {var result =-1, a_item = null; if (this. length = 0) {return result;} for (var I = 0, len = this. length; I <len; I ++) {a_item = this [I]; if (a_item = item) {result = I; break ;}} return result ;}}

2). Object key-Value Pair Method

The execution speed of this method is faster than that of any other method, that is, the memory occupied is larger. Implementation idea: Create a js object and a new array, and traverse the input array, judge whether the value is the key of the js object. If not, add the key to the object and put it into a new array. Note: When determining whether it is a js object key, "toString ()" is automatically executed on the input key. Different keys may be mistakenly considered the same. For example: a [1], a ["1"]. To solve the above problem, you must call "indexOf ".

// The fastest speed, most space occupied (space for time) function unique2 (array) {var n ={}, r = [], len = array. length, val, type; for (var I = 0; I <array. length; I ++) {val = array [I]; type = typeof val; if (! N [val]) {n [val] = [type]; r. push (val);} else if (n [val]. indexOf (type) <0) {n [val]. push (type); r. push (val) ;}} return r ;}

3) array subscript Limit Method

You still have to call "indexOf" to achieve the same performance as method 1. Implementation idea: if the position where the I entry of the current array appears for the first time in the current array is not I, this indicates that the I-th item is repeated and ignored. Otherwise, it is saved to the result array.

Function unique3 (array) {var n = [array [0]; // result array // traverses for (var I = 1; I <array. length; I ++) {// if the position where the I entry of the current array appears for the first time in the current array is not I, // It indicates that the I entry is repeated, ignore. Otherwise, the result array if (array. indexOf (array [I]) = I) n. push (array [I]) ;}return n ;}

4). sort and remove adjacent values

Although the sorting result of the native array's "sort" method is not very reliable, this disadvantage has no effect in removing duplicates without paying attention to order. Implementation idea: sort the input array, and the same value is adjacent after sorting. Then, when traversing, the new array only adds values that are not repeated with the previous value.

// Adjacent the same value, and traverse the function unique4 (array) {array. sort (); var re = [array [0]; for (var I = 1; I <array. length; I ++) {if (array [I]! = Re [re. length-1]) {re. push (array [I]) ;}} return re ;}

5). optimized the traversal array method.

From a foreign blog post, the implementation code of this method is cool; implementation idea: Get the rightmost value that is not repeated and put it into the new array. (If a duplicate value is detected, terminate the current loop and enter the next round of judgment in the top-level loop)

// Train of thought: Get the rightmost value that is not repeated and put it into the new array function unique5 (array) {var r = []; for (var I = 0, l = array. length; I <l; I ++) {for (var j = I + 1; j <l; j ++) if (array [I] === array [j]) j = ++ I; r. push (array [I]);} return r ;}

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 () {// some code var a = "abc";} is omitted here to run the Code as follows: function foo () {var; // 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.

The above are the JavaScript interview questions prepared for you. Have you done well? I hope you can check for missing questions before the interview and pass the examination smoothly.

Articles you may be interested in:
  • Use js to preview uploaded images (TX interview questions)
  • Javascript to preview the uploaded image (TX interview questions)
  • A complete set of javascript interview questions (partial answers)
  • Javascript interview questions from qq
  • Netease JS interview questions and Javascript lexical scopes
  • Renren javascript interview questions can be implemented in advance
  • Learn Javascript object-oriented from Interview Questions (create object)
  • Five typical JavaScript questions

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.