JS interview questions:
[Click here to receive interview question materials]
1. What data types are returned by javascript typeof
string
, boolean
,number
, undefined
, function
,object
2. What are the three types of cast and two types of implicit cast?
Enforce (parseInt, parseFloat, number) implicit (== === + -)
3. Split () and join()
Split () splits a string into an array, and join() converts an array to a string.
Pop (), push(), unshift(), shift()
push()
Add a tailpop()
The tail to deleteunshift()
Add the headshift()
Remove the head
5. What are the compatible writing methods under IE and standards
var ev = ev || window.event
document.documentElement.clientWidth || document.body.clientWidth
Var target = ev.srcElement||ev.target
Copy the code
6. The difference between GET and POST for Ajax requests
get
Is in theurl
behindpost
Put it in the virtual carrierget
Size limitation (only a few parameters can be submitted)post
比get
security- Different applications request data and submit data
7. The difference between call and apply
Object.call(this, obj1, obj2, obj3)
Object.apply(this, arguments)
The only difference between Apply and Call is the way the second argument is passed. Apply’s second argument must be an array, while Call allows a list of arguments to be passed.
8. How do I parse JSON data during Ajax requests
Using JSON. The parse
9. What is event delegation
Using the principle of event bubble, let his triggered event, let his parent element instead of execution!
10. What are closures, what are their features, and how do they affect pages
A closure is a function that reads the internal variables of another function so that the function is not collected by the GC mechanism and can leak memory if it is used too much.
What do you think is a closure portal
11. How do I prevent events from bubbling
- Ie: Prevent bubbling
ev.cancelBubble = true
; - The IE
ev.stopPropagation()
;
12. How do I prevent default events
- 1.
return false
- 2.
ev.preventDefault()
;
13. Add delete replace methods inserted into a contact
1. Create a node
Create a concrete element
createElement();
// Create a text node
createTextNode();
Copy the code
2. Add, remove, replace, insert
appendChild(); / / add
removeChild(); / / remove
replaceChild(); / / replace
insertBefore(); / / insert
Copy the code
3, find
// Pass the label name
getElementsByTagName();
// Pass the value of the element's Name attribute
getElementsByName();
// By element Id, uniqueness
getElementById();
Copy the code
14. Explain how JSONP works and why it is not real Ajax
Dynamically create script tag, callback function
Ajax is data manipulation without a page refresh request
15. Difference between Document Load and Document Ready
Document.onload is executed after the structure and style, external JS, and image are loaded
$().ready(function); $().ready(function);
16. Difference between “==” and “===”
=== will automatically convert the type and determine whether it is equal. === will not automatically convert the type
17. The difference between a function declaration and a function expression?
In Javscript parser when to load the data in the execution environment, the function declarations and function expression is not alike, the parser will first read the function declarations, and make it available before perform any code (access), as for the function expression, you must wait until the parser performs to its line of code, that would truly be parsed.
18. To understand the scope context and this, look at the following code:
var User = {
count: 1.getCount: function() {
return this.count; }};console.log(User.getCount()); / / 1
var func = User.getCount;
console.log(func()); // is 1 and undefined
`func`Is in the`window`Is executed in the context of, so will not be accessed`count`Properties.Copy the code
19. Look at the following code to give the output.
for(var i = 1; i <= 3; i++){ // It is recommended to use let to output the value of I normally
setTimeout(function(){
console.log(i);
},0);
};
4 4 4.// Cause: the Javascript event handler does not run until the thread is idle.
Copy the code
20. When a DOM node is clicked, we want to be able to execute a function. What should we do?
box.onlick= function(){}
box.addEventListener("click".function(){},false);
Copy the code
< button &western nclick ="box()"></button>
Copy the code
21. What is the Javascript event flow model?
“Event bubbling” : Events are first received by the most specific elements and then propagated up the hierarchy. “Event capture” : Events are first received by the least specific nodes and then down the hierarchy to the most specific “DOM event flow” : there are three stages: event capture, target stage, and event bubbling
Look at the following code. What does it output? Explain why.
var a = null;
alert(typeof a);
// object
Copy the code
Explanation: Since the birth of JavaScript, NULL is a data type with only one value, which is NULL. Represents a null pointer object, so typeof detection returns “object”.
23. Check that the string starts with a letter and can be followed by numbers, underscores, or letters. The string is 6 to 30 characters in length
var reg=/ ^ \ [a zA - Z] $/ w {5, 29};
Copy the code
24. What are the values of alert in response to the following codes?
var a = 100;
function test() {
alert(a);
a = 10; // Remove var to define global variables
alert(a);
}
test();
alert(a);
// The correct answer is: 100, 10, 10
Copy the code
25. What is the difference between the two javaScript variable ranges?
- Global variables: valid within the current page
- Local variables: function methods are valid within
26. The difference between null and undefined?
Null is an object representing “none” and is zero when converted to a value; Undefined is a primitive value for “none”, which is NaN when converted to a value. When the declared variable has not been initialized, the default value of the variable is undefined. Null is used to indicate objects that do not yet exist
Undefined means “missing value”, that is, there should be a value here, but it is not defined yet. Typical usage:
1. If a variable is declared but not assigned, it is undefined.
2. When the function is called, the argument that should be provided is not provided, which is equal to undefined.
3, the object has no assigned attribute, the value of this attribute is undefined.
4. If the function returns no value, undefined is returned by default.
Null means “no object”, meaning there should be no value. Typical usage:
1. As parameters of a function, it means that the parameters of the function are not objects.
2. As the end of the object prototype chain.
27. What exactly does the new operator do?
1. Create an empty object that is referenced by this and inherits the prototype of the function.
Properties and methods are added to the object referenced by this.
3. The newly created object is referenced by this and returns this implicitly.
28. What are the ways of lazy loading of JS?
defer
andasync
- Dynamically create
DOM
Mode (Createscript
That is inserted into theDOM
After loadingcallBack
) - Load JS asynchronously on demand
29. What are the advantages and disadvantages of Flash and Ajax?
Flash ajax contrast
1, Flash is suitable for processing multimedia, vector graphics, access to machines; On CSS, processing text is insufficient and not easy to search.
2. Ajax supports CSS, text, and search. Insufficient multimedia, vector graphics, machine access.
Common features: no refresh messaging with the server, user offline and online status, DOM manipulation
30. Without looping, create an array of length 100 with each element equal to its subscript.
new Array(100).fill(0).map((_, c) = > c)
Copy the code
31 What do I do if I want to obtain all the checkboxes in the page? (No third-party frameworks)
var inputs = document.getElementsByTagName("input"); // Get all the input tag objects
var checkboxArray = []; // Initialize an empty array to hold the checkbox object.
for(var i=0; i<inputs.length; i++){var obj = inputs[i];
if(obj.type=='checkbox'){ checkboxArray.push(obj); }}Copy the code
Write a function that clears Spaces before and after strings. (Compatible with all browsers)
String.prototype.trim= function(){
return this.replace(/^\s+/."").replace(/\s+$/."");
}
Copy the code