Javascript基础知识整理
1. javascript where to
a) header or body
<script></script>
b) header:external files
<script src="myScript.js"></script>
2. JavaScript Output
Writing into an alert box, using window.alert().
Writing into the HTML output using document.write().
Writing into an HTML element, using innerHTML.
Writing into the browser console, using console.log().
** Using document.write() after an HTML document is fully loaded, will delete all existing HTML
3. JavaScript is Case Sensitive
Camel Case: LastName
4. JavaScript Statements
If a JavaScript statement does not fit on one line, the best place to break it, is after an operator:
document.getElementById("demo").innerHTML =
"Hello Dolly.";
[** 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.
The safest (but a little slower) way to break a long string is to use string addition(+)
5. JavaScript Operators
a)JavaScript Assignment Operators
Operator | Example | Same As |
---|---|---|
= | x = y | x = y |
+= | x += y | x = x + y |
-= | x -= y | x = x - y |
*= | x *= y | x = x * y |
/= | x /= y | x = x / y |
%= | x %= y | x = x % y |
** | Exponentiation |
~
b)JavaScript Comparison and Logical Operators [JS Comparisons]
Operator | Description |
---|---|
=== | equal value and equal type |
!== | not equal value or not equal type |
~
c)JavaScript Type Operators [JS Type Conversion]
Operator | Description |
---|---|
typeof | Returns the type of a variable |
instanceof | Returns true if an object is an instance of an |