JavaScript is one of the three elements of the Web front-end and is the most popular scripting language on the Internet. JavaScript is a must for a qualified Web front end engineer, and companies look for that knowledge when hiring front end personnel. The following good programmer web front end tutorial will share some common JavaScript interview questions and answers.

1. Undefined and not defined in JavaScript

Using JavaScript without declaring variables throws an exception: var name is not defined. If the exception is not handled, the code stops running. However, typeof undeclared_variable does not raise an exception and returns undefined.

var x; / / declare xconsole. Log (x); //output: undefined

console.log(typeof y); //output: undefined

console.log(z); // ReferenceError: z is not defined

2. What are the disadvantages of creating a truly private method in JavaScript?

Each object creates a private method, which is memory intensive

Code examples:

var Employee = function (name, company, salary) {

this.name = name || “”;

this.company = company || “”;

this.salary = salary || 5000;

// Private method

var increaseSalary = function () {

this.salary = this.salary + 1000;

};

// Public method

this.dispalyIncreasedSalary = function() {

increaseSlary();

console.log(this.salary);

};

};

// Create Employee class object

var emp1 = new Employee(“John”,”Pluto”,3000);

// Create Employee class object

var emp2 = new Employee(“Merry”,”Pluto”,2000);

// Create Employee class object

var emp3 = new Employee(“Ren”,”Pluto”,2500);

Here emp1, emp2, and emp3 each have a copy of the increaseSalary private method, so using it unless necessary is not recommended.

3, How to determine whether an Object is an array?

Methods a

The use of the Object. The prototype. To judge whether the toString array

function isArray(obj){

return Object.prototype.toString.call( obj ) === ‘[object Array]’;

}

Call is used here to make this in toString point to obj, thereby completing the judgment.

Method 2

Use the prototype chain to complete the judgment

function isArray(obj){

return obj.__proto__ === Array.prototype;

}

The basic idea is to use an instance, and if it’s constructed by a constructor its __proto__ refers to the constructor’s Prototype property.

Methods three

Using the JQuery

function isArray(obj){

return $.isArray(obj)

}

The JQuery isArray implementation is actually method 1.

4. What does the following code output?

var output = (function(x){

delete x;

return x;

}) (0);

console.log(output);

The output is 0. The delete operator deletes the property of an object. But x is not an object property, and the delete operator does not work.

5. How to understand this keyword in JS?

JS beginners are always confused by the this keyword because it is a bit tricky in JS compared to other modern programming languages. “This” generally refers to the current object, but things don’t happen as they should. The JS this keyword is determined by the caller of the function, who calls this refers to which. If the caller is not found, this points to a Windows object.

How do I compare two objects in JavaScript?

For two non-primitive values, such as two objects (including functions and arrays), == and === comparisons only check whether their references match, not the actual contents of the references.

For example, arrays are forced to be converted to strings by default, and all elements of the array are concatenated using commas. So, two arrays with the same contents will not be equal when compared == :

Var a = [1, 2, 3];

Var b = [1, 2, 3];

Var c = “1, 2, 3”;

a == c; // true

b == c; // true

a == b; // false

For depth comparisons of objects, you can use the deep-equal library, or you can implement your own recursive comparison algorithm.

Explain prototyping patterns

The prototype pattern can be used to create new objects, but instead of creating uninitialized objects, it creates objects that are initialized with the values of the prototype object (or sample object). The stereotype pattern is also known as the attribute pattern.

The prototype pattern is useful when initializing a business object whose value matches the default value in the database. The default values in the prototype object are copied to the newly created business object.

Classical programming languages rarely use the stereotype pattern, but JavaScript, as a prototype language, uses it when constructing new objects and their prototypes.

Of course, these are just a few of the classic JavaScript interview questions, but also a few of the web front-end engineer interview questions. If you want to become a well-paid Web talent, you should not only have solid theoretical knowledge, but also have a lot of practical project experience.