Home Web Front-end JS Tutorial Some basic questions about JS

Some basic questions about JS

May 21, 2018 am 10:21 AM
javascript Base question

We often encounter some js problems during development and learning. This article will introduce some related js basic issues.

What is the order in which CSS and JS are placed on the web page?

Generally we put the CSS in the head tag and the JS code at the end of the body code.

<!DOCTYPE html><html><head>
  <meta charset="utf-8">
  <title>JS基础</title>
  <style>
    
    /* 这里放css代码 */      
    
  </style></head><body>
  <!-- 这里放HTML代码 -->
    
  <script>
Copy after login




Explanation of white screen and FOUC

White screen: If you put the CSS style at the bottom, in some scenarios (new window opening, refresh, etc.) the page will appear with a white screen instead of the content being displayed gradually. If you use @ Import tag, even if the CSS link is placed and placed in the header, a white screen may occur.

The script will block the display of subsequent content

The script will block the download of subsequent components
For images and CSS, they will be loaded concurrently during loading (such as loading two at the same time under one domain name) file), but when loading JS, concurrency is disabled and other content is prevented from being downloaded, so placing JS at the top of the page will also cause a white screen.

FOUC (Flash of Unstyled Content) Unstyled content flash: If you put the CSS style at the bottom, for IE browser, in some scenarios (click on the link, enter the URL, use the bookmark to enter, etc.), it will The FOUC phenomenon will occur (the unstyled content is gradually loaded, and the page suddenly displays the style after the CSS is loaded). For Firefox, the FOUC phenomenon will always occur.

What are the functions of async and defer? What's the difference?

Without async and defer, the current script will be loaded and executed immediately during browser loading, and the browser will be blocked from rendering the document elements under the script tag. However, with async and defer, the browser can The process of loading and rendering subsequent documents is carried out in parallel with the loading and execution of JS scripts, that is, loading is asynchronous.

The difference between the two is mainly when the script is executed after it is downloaded. defer will delay the execution of the script according to the loading order until the document is parsed and displayed, while async will not matter the order of your declaration, as long as it is loaded. Execute immediately, not in order.
Generally speaking defer is more in line with our usage scenarios.

Rendering mechanism of web pages

Parses HTML tags and builds DOM trees

Parses CSS tags and builds CSSOM trees

Combines the DOM and CSSOM into a rendering tree (render tree) perform basic layout

Calculate the geometric structure of each node

Draw each node to the screen and present it to the user

JavaScript defines several data types ? Which are simple types? Which are complex types?

Null: It is a null pointer

Underfined: There is a pointer but it does not point to any space

Boolean: Boolean value, true or false (true,false)

Number: Numeric type.

String: String type, represented by single quotes or double quotes.

Object: Object, the core concept of JS, the most important data type. (Everything is an object)

The first five types are simple types, and Object is a complex type.

What do NaN, undefined, and null represent respectively?

NaN: The meaning is Not a number, indicating a non-number, not equal to any value, including himself, (one is not equal to any Numeric type of value)

underfined: It is the only value of Underfined, which means that the variable is only declared but not initialized, which means that there is this pointer, but this pointer does not point to any space

null: Null The only value represents a null pointer, which is something that does not exist.

The functions and differences between typeof and instanceof?

The function of typeof can determine the data type of a value returned and put it in the operation In front of the number, the operand can be of any type.

function a(){}
a(){}typeof a"function" //The operand is the function returning functiontypeof 123421"number"typeof "32423423423""string"typeof true"boolean" typeof undefined"undefined" //Use this to check undefined variables typeof window"object" //All others return object

instanceof is used to determine whether a variable is an instance of an object, because typeof encounters The object type will be returned for null, array, and object, so when we want to determine whether an object is an array, or determine whether a variable is an instance of an object, we must use instanceof

Code Problem

1. The following code determines whether a variable is a number, string, Boolean, or function

function isNumber(el){ 
 if ((typeof el)==="Number"){   return true;
   }  else { 
return false
  };
}function isString(el){ 
  if ((typeof el)==="String"){   return true;
   }  else { 
return false
  };
}function isBoolean(el){ if ((typeof el)==="Boolean"){   return true;
   }  else { 
return false
  };
}function isFunction(el){  if ((typeof el)==="Function"){   return true;
   }  else { 
return false
  };
}var a = 2, 
b = "jirengu", 
c = false;alert( isNumber(a) ); //truealert( isString(a) ); //falsealert( isString(b) ); //truealert( isBoolean(c) ); //truealert( isFunction(a)); //falsealert( isFunction( isNumber ) ); //true
Copy after login

2. The output result of the following code is?

Some basic questions about JS

##I am a picture

3.The output result of the following code is?

var a = 1;

a a;typeof a 2;

The result is number2, (typeof has a very high priority, so typeof a is calculated first and the result is number, and 2 is included, so it is number2)

4. Traverse the array and print each element in the array. Square of one term

var arr = [3,4,5]// todo..// Output 9, 16, 25


The code is as follows:

Some basic questions about JS

I am a picture

5. Traverse JSON and print the values ​​inside

var obj = { name: 'hunger', sex: 'male', age: 28}//todo ...// Output name: hunger, sex: male, age:28

The code is as shown in the picture: (reference here)

Some basic questions about JS

I am the picture

6. What is the output of the following code? Why

console.log(a);var a = 1;console.log(a);console.log(b);

The first console.log(a ); The output is underfined because JS will promote global variables, and a is only declared but not assigned.
The second console.log(a); output is 1, because it assigns 1 to a after var a =1;.
The third console.log(b); output will report an error because b is not declared or assigned a value.

This article introduces some basic introduction issues. If you want more related knowledge, please pay attention to the php Chinese website.

Related recommendations:

How to use front-end js to modularize require.js

A picture implemented with CSS Completed button example

Related knowledge about AJAX ASP/PHP request example

The above is the detailed content of Some basic questions about JS. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems

How to solve the problem that jQuery cannot obtain the form element value How to solve the problem that jQuery cannot obtain the form element value Feb 19, 2024 pm 02:01 PM

How to solve the problem that jQuery cannot obtain the form element value

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecasting system

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

Simple JavaScript Tutorial: How to Get HTTP Status Code

What are the questions in the Rulong 8 Wine Master exam? What are the questions in the Rulong 8 Wine Master exam? Feb 02, 2024 am 10:18 AM

What are the questions in the Rulong 8 Wine Master exam?

How to get HTTP status code in JavaScript the easy way How to get HTTP status code in JavaScript the easy way Jan 05, 2024 pm 01:37 PM

How to get HTTP status code in JavaScript the easy way

What happens if there is a problem with the sound card driver? What happens if there is a problem with the sound card driver? Mar 02, 2024 am 10:49 AM

What happens if there is a problem with the sound card driver?

Analysis of common problems when Linux SSH service fails to start Analysis of common problems when Linux SSH service fails to start Mar 19, 2024 pm 05:48 PM

Analysis of common problems when Linux SSH service fails to start

See all articles