Home > Web Front-end > JS Tutorial > JavaScript learning path

JavaScript learning path

巴扎黑
Release: 2017-07-17 14:57:12
Original
1212 people have browsed it

1. Floating point numbers will produce errors during operations because computers cannot accurately represent infinitely recurring decimals.
To compare whether two floating point numbers are equal, you can only calculate the absolute value of their difference to see if it is less than a certain threshold (an acceptable range):
Math.abs(1 / 3 - (1 - 2 / 3)) < 0.0000001; // true

##2.null and undefined

null represents an "empty" value, which is different from 0 and the empty string'', 0 is a Numeric value, '' means a string of length 0, and null means "empty".
In JavaScript, there is also undefined, which is similar to null, which means "undefined". The designers of JavaScript intended to use null to represent an empty value, and undefined to represent an undefined value.
Facts have proved that this is of no use, and the difference between the two is of little significance. In most cases, we should use null. undefined is only useful when determining whether function parameters are passed.

3.strict mode

At the beginning of JavaScript design, in order to facilitate beginners to learn, it was not mandatory to use var to declare variables.
This design error has serious consequences: if a variable is used without var declaration, then the variable is automatically declared as a global variable:
i = 10; // i is now a global variable
In different JavaScript files on the same page, if they do not use var declarations and happen to use variable i, the variables i will affect each other and produce erroneous results that are difficult to debug.
A variable declared using var is not a global variable. Its scope is limited to the function body in which the variable is declared. Variables with the same name do not conflict with each other in different function bodies.
In order to fix this serious design flaw in JavaScript, ECMA introduced strict mode in subsequent specifications.
JavaScript code running in strict mode is forced to declare variables through var. If a variable is used without using var, it will be used. Cause running errors.
The way to enable strict mode is to write in the first line of JavaScript code:
'use strict';
This is a string. Browsers that do not support strict mode will treat it as a string. Statement execution, browsers that support strict mode will enable strict mode to run JavaScript.

To test whether your browser can support strict mode:

'use strict';
// If the browser supports strict mode,
// The following code will report a ReferenceError Error:
abc = 'Hello, world';
alert(abc);

4. Multi-line string - backtick

Because multi-line strings are written with \n It is more troublesome, so the latest ES6 standard adds a new method of expressing multi-line strings, using backticks ` ... ` (the key below esc) to express:
`This is a
multi-line
String`;

5. Template string--${variable name}

ES6 adds a new template string, the representation method is the same as the above multi-line string, but it Variables in the string will be automatically replaced:
var name = 'Xiao Ming';
var age = 20;
var message = `Hello, ${name}, you are ${age} years old this year !`;
alert(message);

6.JavaScript treats null, undefined, 0, NaN and empty string '' as false, and all other values ​​​​are treated as true.

7.swtich is a JavaScript keyword and cannot be used as a function name

The above is the detailed content of JavaScript learning path. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template