7 JavaScript interview questions to eliminate the false and keep the True

  • 2020-11-20 06:01:06
  • OfStack

Here are 7 JavaScript interview questions you should ask before your interview. Otherwise, you're likely to waste your time.
1. What are the two ways to create JavaScript objects?
This is a very simple question, if you've ever used JavaScript. You have to know at least one way. Still, in my experience, there are plenty of people who claim to be JavaScript programmers who don't know how to answer this question.

Use the "new" keyword to call the function. open/close curly braces.

var o = {};
You can also go ahead and ask, "When is an object created using the new keyword?" However, since I just want to eliminate 1 person, I will wait for the actual interview to ask these questions.
2. How to create an array?
This is the same level of question as "How to create objects." However, there are also some people who can answer the first question, but cannot answer this question.
With the following code, you can simply create an array:
var myArray = new Array();
Creating an array is a complex process. But I would like to hear an answer in square brackets from a candidate.
var myArray = [];
Of course, we could go on to ask other questions, such as how to efficiently remove duplicate elements from the JavaScript array, but since we only need to know if the candidate is worth the one-step observation, I'll stop there.

How to efficiently remove duplicate elements from JavaScript array: js implements an array to reorganize five methods.

Specific methods:

1) Multiple group method

The simplest de-duplication method, to achieve the idea: create 1 new array, pass through the array, the value of the new array is not added to the new array; Note: The method "indexOf" to determine whether the value is in the array is ECMAScript5 method, IE8 is not supported below, need to write 1 more compatible low-version browser code, the source code is as follows:


//  The simplest method of array deduplication 
function unique1(array){
 var n = []; //1 A new temporary array 
 // Iterate through the current array 
 for(var i = 0; i < array.length; i++){
  // If the number of the current array i I've saved it into a temporary array, so I'm going to skip, 
  // Otherwise put the current item push Go to the temporary array 
  if (n.indexOf(array[i]) == -1) n.push(array[i]);
 }
 return n;
}
//  Determine if the browser supports it indexOf  . indexOf  for ecmaScript5 The new method  IE8 The following (including IE8 .  IE8 Support only part ecma5 ) does not support 
if (!Array.prototype.indexOf){
 //  new indexOf methods 
 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

This method executes faster than any other method, but takes up a little more memory. Implementation idea: create 1js object and new array, pass through the array, judge whether the value is the key of js object, if not, add the key to the object and put the new array. Note: When judging whether it is an js object key, "toString()" will be automatically executed on the passed key. Different keys may be mistaken for one; For example: a[1], a["1"]. To solve the above problem, call "indexOf" again.


//  The fastest,   Takes up the 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 index judgment method

If the first occurrence of the i item in the current array is not i, then the i item is repeated and ignored. Otherwise, put it in the result array.


function unique3(array){
 var n = [array[0]]; // The result array 
 // From the first 2 The term starts traversing 
 for(var i = 1; i < array.length; i++) {
  // If the number of the current array i The item is the first in the current array 1 The secondary position is not i . 
  // So this is the regulation i The terms are repeated. Ignore them. Otherwise, put it in the result array 
  if (array.indexOf(array[i]) == i) n.push(array[i]);
 }
 return n;
}

4). Adjacent division after sorting

Although the "sort" method of a native array doesn't make much sense, it doesn't matter in the case of sequence-agnosing. Implement the idea: to the incoming array sort, sort the same value adjacent, and then traversed through the new array only to add the value of the previous 1 value.


//  Adjacent to each other, the same values are then iterated over 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) Optimize the traversal group method

From foreign blog posts, the code for this method is pretty cool; Get the rightmost value that does not repeat into the new array. (Terminates the current cycle and enters the next round of judgment of the top cycle when a duplicate value is detected)


//  Idea: Get the right most that is not repeated 1 Put the value 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 promotion (Variable Hoisting)?
This is a slightly more difficult question, and I'm not asking for a definite answer. But it's a quick way to determine a candidate's skill level: Do they really understand the programming language as well as they claim?
Variable promotion means that wherever a variable is declared within a range, the JavaScript engine moves this declaration to the top of the range. If you declare a variable in the middle of a function, for example, assign a variable on a line 1:


function foo()
{
 //  Some code is omitted here 
 var a = "abc";
}
 This actually runs the code like this: 
function foo()
{
 var a;
 //  Some code is omitted here 
 a = "abc";
}

4. What are the risks of global variables and how can you protect your code from interference?
The danger with global variables is that others can create variables with the same name and then override the variables you are using. This is a headache in any language.
There are many ways to prevent it. The most common method is to create a global variable that contains all the other variables:
var applicationName = {};
Then, whenever you need to create a global variable, append it to the object.
applicationName.myVariable = "abc";
Another option is to wrap all the code into a function that automatically executes, so that all declared variables are declared within the scope of the function.


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

In reality, you'll probably use both.
5. How do I iterate through member variables in an JavaScript object?


for(var prop in obj){
 // bonus points for hasOwnProperty
 if(obj.hasOwnProperty(prop)){
  // do something here
 }
}

6. What are closures (Closure)?
Closures allow a function to be defined within the scope of another external function, allowing it to access variables within that external function even when everything else in the scope has disappeared. Candidates should be awarded points if they can explain the risks of using closures in for/next loops without declaring variables to hold the current value of iteration variables.
7. Describe the JavaScript unit tests you have had.
With that in mind, we just want to see if the candidate has actually done the JavaScript unit test. This is an open-ended question. There is no right answer, but the person must be able to tell at least a few things about the process.

The above is the JavaScript interview question prepared for you. Did you do it well? I hope you can check and fill in the blanks before the interview and pass the interview smoothly.


Related articles: