JavaScript notes 2

Source: Internet
Author: User

1. Floating-point numbers generate errors during operation because the computer cannot accurately represent infinite loop decimals.
To compare two floating-point numbers for equality, you can only calculate the absolute value of their difference to see if they are 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 differs from 0 and an empty string ' ', 0 is a numeric value, ' ' represents a string of length 0, and null denotes null.
In JavaScript, there is also a null-like undefined, which means "undefined." The designer of JavaScript wants NULL to represent an empty value, while undefined indicates that the value is undefined.
It turns out that there is no such thing as an egg, and the difference between the two is insignificant. In most cases, we should all use NULL. Undefined is useful only when judging whether a function parameter is passed or not.

3.strict mode
JavaScript at the beginning of the design, in order to facilitate learning for beginners, does not require the use of VAR declaration variables.
This design error has serious consequences: if a variable is used without a var declaration, the variable is automatically declared as a global variable:
i = 10; I am now a global variable
In the same page of the different JavaScript files, if you do not use the Var declaration, exactly all using the variable I, will cause the variable i affect each other, resulting in difficult to debug error results.
A variable declared with VAR is not a global variable, its scope is limited to the function body declared by the variable, and the variable of the same name does not conflict with each other in the function body.
In order to fix the serious design flaw of JavaScript, ECMA introduced the strict model in the follow-up specification,
JavaScript code that runs in strict mode, which enforces variables declared through Var, is used without using VAR to declare variables and will result in run errors.
The way to enable strict mode is to write on the first line of the JavaScript code:
' Use strict ';
This is a string, browsers that do not support strict mode will execute it as a string statement, and browsers that support strict mode will turn on strict mode to run JavaScript.

To test if 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--anti-quote
Because multiline strings are more cumbersome to write, the newest ES6 standard adds a multiline string representation, using the anti-quote ' ... ' (key below ESC) to indicate:
' This is a
MultiRow
String ';

5. Template string--${variable name}
ES6 adds a template string that represents the same method as the multiline string above, but it automatically replaces the variables in the string:
var name = ' Xiao Ming ';
var age = 20;
var message = ' Hello, ${name}, you ${age} year old! ';
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

JavaScript notes 2

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.