- Variables created without the keyword var, are always global, even if they are created inside a function.
- A closure is a function having access to the parent scope, even after the parent function has closed.
- A JavaScript function can be invoked without being called.
-
Accessing a function without () will return the function definition:.
<script> function toCelsius(f) { return (5/9) * (f-32); } document.getElementById("demo").innerHTML = toCelsius; </script>
输出:
function toCelsius(f) { return (5/9) * (f-32); }
- Avoid String, Number, and Boolean objects. They complicate your code and slow down execution speed.
- If you assign a value to a variable that has not been declared, it will automatically become a GLOBAL variable, even if it is executed inside a function. Do NOT create global variables unless you intend to.
- In HTML, the global scope is the window object: All global variables belong to the window object.
-
Your global variables (or functions) can overwrite window variables (or functions).
Any function, including the window object, can overwrite your global variables and functions. - W3Schools JavaScript Reference HTML DOM Events
-
You can also break up a code line within a text string with a single backslash:
document.getElementById("demo").innerHTML = "Hello \ Dolly!";
(The \ method is not a ECMAScript (JavaScript) standard.Some browsers do not allow spaces behind the \ character.)
- Integers (numbers without a period or exponent notation) are considered accurate up to 15 digits. The maximum number of decimals is 17, but floating point arithmetic is not always 100% accurate:
- In JavaScript, all data types have a valueOf() and a toString() method.
- JavaScript counts months from 0 to 11. January is 0. December is 11.
- Bit operators work on 32-bit numbers.
- The constructor property returns the constructor function for all JavaScript variables.
- The unary + operator can be used to convert a variable to a number:
-
5 + null // returns 5 because null is converted to 0
"5" + null // returns "5null" because null is converted to "null"
"5" + 2 // returns 52 because 2 is converted to "2"
"5" - 2 // returns 3 because "5" is converted to 5
"5" * "2" // returns 10 because "5" and "2" are converted to 5 and 2 - Complete JavaScript RegExp Reference
- Hoisting is JavaScript’s default behavior of moving all declarations to the top of the current scope (to the top of the current script or the current function).
- Do not use tabs (tabulators) for indentation. Different editors interpret tabs differently.
- Avoid global variables, avoid new, avoid ==, avoid eval()
JavaScript里的奇葩语法
排名不分先后。
-
"1234"+5
的结果是"12345"
, 而"1234"-4
的结果却是1230
.4+"1234"
的结果是"41234"
, 而4-"1234"
的结果是NaN
. -
true==2
的结果是false
,false==2
的结果也是false
. -
typeof null
的结果是"object"
. -
对于
str.slice(start, end)
,如果参数大于等于0,则结果不包括end
指示的字符;如果参数小于0,则结果不包括start
指示的字符. -
var x = 0.2 + 0.1; // x will be 0.30000000000000004
-
typeof NaN; // returns "number"
var x = NaN + "5"; var y = Infinity + "3"; // x will be "NaN5", and y will be "Infinity3"
-
0 < Number.MIN_VALUE // true
-
var txt = "";
var person = ["23", "12f", 2];
var x;
for (x in person) {
txt += typeof x + " ";
}
最成txt为string string string