Language Nature
JavaScript (JS) functions as a lightweight, interpreted, or just-in-time compiled scripting language embedded in host environments. It relies on an event-driven model to handle user interactions without requiring server round-trips.
Key Characteristics
- Object-based design: Code organizes around objects, allowing developers to create, modify, and reuse entities to keep logic modular.
- Loose typing: Variable types are determined at runtime. A single variable can hold a number initially and later store a string without explicit conversion.
- Dynamic behavior: The runtime responds immediately to user actions such as clicks, keystrokes, or scroll events.
- Cross-environment execution: Any compliant browser or runtime can parse and execute the same JS code regardless of the underlying operating system.
Integration Methods
- Inline scripts: Place JavaScript directly inside
<script>tags within an HTML document. - External scripts: Store code in standalone
.jsfiles and link them via thesrcattribute.
Example:<script src="../scripts/app.js"></script>
Syntax Fundamentals
Variable Declarations
JavaScript's weak type system means variables adapt to whatever value they hold. Naming conventions permit letters, digits, underscores, and dollar signs, but a name must not start with a digit. CamelCase notation is recommended.
Declarations use var, let, or const:
var score = 20;
score = "张三";
| Keyword | Behavior |
|---|---|
var |
Function-scoped, can be redeclared |
let |
Block-scoped, prevents redeclaration |
const |
Block-scoped constant, immutable binding |
Value Categories
Primitive types and reference types compose the data model:
| Primitive | Description |
|---|---|
number |
Integers, floats, NaN (Not a Number) |
string |
Single- or double-quoted text |
boolean |
true / false |
null |
Represents an intentional absence of any object |
undefined |
Variable declared but not assigned a value |
BigInt |
Integers beyond the safe range of number |
Run typeof on an expression to inspect its current data type.
Type Coercion Rules
| Source → Target | Conversion Logic |
|---|---|
string → number |
Numeric string becomes its value; otherwise NaN |
boolean → number |
true → 1, false → 0 |
number → boolean |
0 and NaN become false; everything else true |
string → boolean |
Empty string → false; non-empty → true |
null → boolean |
Always false |
undefined → boolean |
Always false |
parseInt() is frequently used to convert strings to integers when strict numeric values are required.
Operators
Most operators mirror those in Java:
| Category | Symbols |
|---|---|
| Unary | ++, -- |
| Arithmetic | +, -, *, /, % |
| Assignment | =, +=, -= |
| Relational | >, <, >=, <= |
| Logical | &&, ` |
| Ternary | condition ? expr1 : expr2 |
Equality comparison details:
==will coerce types if they differ before comparing values.===(strict equality) returnsfalseimmediately when types do not match, comparing values only when types are identical.
Control Flow
Conditional and loop structures (if...else, for, while, switch) follow the same semantics as Java.
Functions
A function packages a reusable block of logic. Declare one with the function keyword:
function computeTotal(price, quantity) {
return price * quantity;
}
Parameter types are omitted; the function does not require a return type annotation—simply use return to produce a value.
Invocation example:
let invoice = computeTotal(15, 4);
Functions accept any number of arguments. Extra arguments beyond the defined parameters are ignored, while missing parameters take the value undefined.
Consider a function expression:
var combine = function(a, b) {
return a + b;
};
let output = combine(10, 20, 30);
In this scenario, a receives 10, b receives 20, and the third argument 30 is discarded. The call therefore evaluates to 30.
Common Output Mechanisms
- Alert dialog:
alert('message')displays a popup (thewindow.prefix is optional). - Document rendering:
document.write('content')inserts output directly into the HTML stream. - Console logging:
console.log('data')writes debug information to the browser's developer tools.