In the development process, there are always some details that are ignored by us. This series is to review JS and tell you some details that are ignored. I hope you can communicate more.

Points to note about the script tag

  • The tag can directly write js code, here must avoid internal ” string, otherwise the browser will report an error. Because browsers parse in-line scripts to treat the string ” as a closing tag, to avoid this problem, we need to prefix the string with the \ escape character ‘\’.

  • A set of tags that import an external script and write an internal script at the same time, our tag will ignore the internal JS code and only perform the import of the external script.

  • The SRC attribute of the tag can be used by Jsonp cross-domain implementations to import files not only in the same domain but also in scripts that are not in the same domain. When the browser parses the resource, it sends a GET request to the URL to get the resource. Make sure that the scripts in the external domain are trusted

  • The tag is usually placed at the end of the HTML code in the tag. This is to avoid loading scripts in the tag, resulting in a long blank period of the page, which reduces the user experience

Is it recommended to introduce external scripts or internal scripts during development?

External scripts

External scripts have the following advantages over internal scripts:

  1. The code is more maintainable and relatively independent.
  2. When the browser introduces the same script file to multiple pages, the cache used for external import is lower, and internal import may need to be opened several times, while external import only needs to be opened once.

Variables declare var and let, const

  • No matter var or let is used, the declared variables are all undipay if no value is assigned

  • The scope of a variable declared with var in a function body exists only within the function itself. The function will be destroyed after execution. If you do not use the keyword, a global variable will be created inside the function body

  • Variables declared by the var keyword are promoted to the top of the function scope during the execution of the function.

Function foo() {console.log(name) var name = 'console.log'} Function foo() {var name console.log(name) name = 'illegally pay'} So I pay therefore I am not undefinedCopy the code
  • varandletThe difference between
  1. varDeclaration applied to function scope,letDeclares an effect on the block-level scope.
If (true){var name = 'zhang 3' console.log(name) // Zhang 3} console.log(name) {let name = 'Zhang 3' console.log(name) / / zhang SAN} the console. The log (name) / / name undefined error Here let's statement variables will not be able to make calls outside if block, because it is relative to the block level scopeCopy the code

2. The variables declared by var have variable promotion, but the variables declared by let have no variable promotion.

Therefore, I am 25; therefore, I am 25; therefore, I am 25; therefore, I am 25; therefore, I am 25; therefore, I am 25; therefore, I am 25; therefore, I am 25. Name undefined let name = '3' Before the let declaration variable code, known as a "temporary dead zone", in this phase reference to any variable declared later will raise ReferenceErrorCopy the code

3. Variables declared by var in the global scope become properties of the window object, but variables declared by let do not

Under the global scope: var name = 'zhang 3' console.log(window.name) // Zhang 3 let age = 999 console.log(window.age) // UndiameiCopy the code

4. If you declare the same variable multiple times in the same scope, var does not report an error because variable promotion merges the same variables into one. Let will report an error because the scope is a block, so there is no way to know if a variable with the same name has been declared before.

5. In the for loop, as well as in the for-in and for-of loops, suppose there is an iteration variable of I. If the iteration variable is declared with var, the value of I can be obtained outside the loop.

6. When you declare an iteration variable with var, the iteration variable holds the value that causes the loop to exit. When you declare an iteration variable with let, the JS engine declares a new iteration variable behind the scenes for each iteration loop.

for(var i = 0 ; i < 5 ; I ++){setTimeout(()=> console.log(I),0)} i < 5 ; I ++){setTimeout(()=> console.log(I),0)} Output: 0 1 2 3 4Copy the code
  • constDeclared variables must be initialized at the same time, and when you modifyconstVariables that are declared will cause runtime errors, and are not allowed to be repeatedly declared. The scope is also block-level scope
  • constIn other words, if you declare an object, there is no restriction on modifying properties in the object
  • constCannot be used to declare iterated variables because iterated variables change over time.

How to declare the best choice

  • Do not usevarDeclaration, onlyletandconstYou can avoidvarBring about all kinds of problems
  • letTo declare variables that will change in the future,constTo declare a variable that will not change in the future, which can be interpreted as declaring a constant

Type of data

Number String Boolean Undefined Null Symbol

Reference data types include Object Function Array

What typeof data can be detected using typeof methods

Note: Typeof (NULL) displays type Object because NULL is considered a reference to an empty Object

Various types of points to note

Undifined type

  • Undifined typeThere’s only one valueundifinedWhen a variable is declared but not initialized, it is equivalent to assigning a value to a variableundifined
  • Points to note when initializing undeclared and declared variables
let name; I therefore pay 25 yuan, therefore I therefore pay 25 yuan; console.log(name) // Undi25 therefore I pay 25 yuan; console.log(typeof name) //undifined console.log(typeof age) //undifinedCopy the code

By default, when TypeOf detects an undeclared variable, the undipay type is displayed

Null type

  • Null typeAgain, there’s only one valuenullIs considered a reference to an empty object,Typeof (null)forObject
  • null == undifinedreturntrueThis is because == converts operands

Boolean type

  • BooleanThere are two literalstrueandfalseUnlike numbers, true is not equal to 1 and false is not equal to 0
  • Booleans are case sensitive, so True and False are not Booleans
  • Different types of Boolean conversions
Boolean values that can be converted to true include: String non-empty String Number non-zero value (including infinity) Object arbitrary Object Undefined N/A(does not exist) False Boolean values include: String "" (empty String) Number 0, NaN Object null Undefined Undefined this is used after the if condition, will automatically convert the data type to a Boolean valueCopy the code

The Number type

  • 0.1 + 0.2! = 0.3 Because IEEE 754 values are used, there is a rounding error because you would normally get 0.30000000000000004

  • ECMAScript has a range of values that can be expressed in ECMAScript. Any number outside this range is automatically converted to Infinity(-infinity means negative Infinity, Infinity means positive Infinity). Returns true if yes, false if no

  • The special value NaN, which literally means “not a number”, is used to indicate that an operation intended to return a number failed. Any operation involving NaN always returns NaN, and NaN is not equal to any value including NaN.

    Numerical transformation

Number() method conversion rule:

  • Boolean values: true to 1, false to 0
  • Value: Returns directly
  • Null: Returns 0
  • Undefined: returns NaN
  • String: Numeric strings automatically erase the preceding 0 and convert it to a decimal number. NaN is returned for all but numeric strings

ParseInt () method conversion rule:

  • Numeric strings are converted to integers and automatically recognize the base number, receiving the specified base number as the second argument.
  • NaN is returned if the first character is not numeric.

Type String

  1. The string length islengthProperty, availablestr.lengthTo obtain.
  2. In addition tonullandundifined, other types are availabletoString()Method to a string, for examplea.toString(), and toString(base number) takes a base value as an argument. The value type can get the corresponding base number string.
  3. Determines whether a value isnullorundifinedYou can useString()Methods.

Symbol type

  • Symbol(symbol) isES6A new data type is used to create a unique representation of an object property as a string. Specific you can consult the document to understand.

The Object type

Object instances have the following attributes and methods: let obj = new Object()

  • constructor: The function used to create the current object
  • hasOwnProperty(propertyName): Determines whether a given attribute exists on the current object instance
  • isPrototypeOf(Object): Used to determine whether the current object is a prototype of another object
  • propertyIsEnumerable(propertyName): Determines whether a given attribute is available
  • toLocaleString(): Returns a string representation of the object (reflecting the localized execution environment of the object)
  • toString(): Returns a string representation of an object
  • valueOf(): Returns the string, numeric or Boolean representation of the object, normally the same as toString()