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:

//Simplest array de duplication
function unique1(array){
Var n = []; / / a new temporary array
//Traverse current array
for(var i = 0; i < array.length; i++){
//If I of the current array has been saved into the temporary array, skip,
//Otherwise, push the current item into the temporary array
if (n.indexOf(array[i]) == -1) n.push(array[i]);
}
Return n;
}
//Determine whether the browser supports indexof. Indexof is a new method of ecmascript5 under IE8 (including IE8, which only supports part of ecma5). It is not supported
if (!Array.prototype.indexOf){
//New 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 ".

//Fastest, most space (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
//Traverse from the second item
for(var i = 1; i < array.length; i++) {
//If item I of the current array first appears in the current array at a position other than I,
//Then it means that item I is repeated and ignored. Otherwise, it will be stored in 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 then traverse to remove duplicate values
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)

//Idea: get the rightmost value without repetition 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()
{
//Several codes are omitted here
var a = "abc";
}
You actually run the code like this:
function foo()
{
Var a;
//Several codes are omitted here
A = "ABC";
}

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

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.