- ECMAScript: JavaScript syntax
- DOM: Document Object Model
- BOM: Browser Object Model
1. ECMAScript
ECMAScript is a standardized programming language developed by ECMA International (formerly the European Computer Manufacturers Association). It is widely used on the World Wide Web and is often referred to as JavaScript or JScript, although these are implementations and extensions of the ECMAScript language.
ECMAScript defines the programing syntax and fundamental knowledge of JavaScript. It is an industry standard for JavaScript syntax that all browser manufacturers follow.
2. DOM - Document Object Model
The Document Object Model (DOM) is a standard programming interface recommended by the W3C organization for processing extensible markup language. Through the DOM interface, you can manipulate various elements on the page (such as size, position, color, etc.).
3. BOM - Browser Object Model
The Browser Object Model (BOM) refers to the object structure that allows interaction with the browser window. Using BOM, you can control the browser window, such as displaying pop-up windows, controlling navigation, and retrieving screen resolution.
- Variables
- Why use variables? Because we need to store some data.
- What is a variable? A variable is a container used to store data, making it easy to access the data later.
- What is the essence of a variable? A variable is a block of memory space used to store data.
- How to use a variable? When using a variable, you must declare it first and then assign a value. Declaring a variable essentially means allocating memory space.
- What is variable initialization? Declaring a variable and assigning a value is called variable initialization.
- What are the naming conventions for variables? Variable names should be clear and meaningful, following camel case notation. Also, identify invalid variable names.
- How to exchange values between two variables? Learn how to swap the values of two variables.
- Data Types
JavaScript is a weakly typed or dynamic language.
JS divides data types into two categories:
- Primitive data types (Number, String, Boolean, Undefined, Null)
- Complex data types (Object)
| Primitive Data Type | Description | Default Value |
|---|---|---|
| Number | Numeric type, includes integer and floating-point values, such as 21, 0.21 | 0 |
| Boolean | Boolean type, such as true, false, equivalent to 1 and 0 | false |
| String | String type, such as "Zhang San", note that in JS, strings are always enclosed in quotes | "" |
| Undefined | var a; declares variable a but does not assign a value, so a = undefined | undefined |
| Null | var a = null; declares variable a as null | null |
isNaN(x) returns true if x is not a number, otherwise false;
Loooping Through a String
// Loop through a string
exec is typically used for standalone actions that run once, returning query(find() method returns data)
'Sname': new RegExp(.)
newData = results.filter((item) => item.commodityIntroduction.match(RegExp(/i7/)));
- Data Type Conversion
1. Convert to String
- num.toString()
- String(num) //强制转换
- num + ''
2. Convert to Number
- parseInt(str) // integer
- parseFloat(str) // decimal
- Number(str) //强制转换 (str must be numeric string)
- 'str' - 0 = str // subtraction, multiplication, division (str must be numeric string)
String to number conversion: "6" + 0 ==> 6
3. Convert to Boolean
- Values representing empty or negative values will convert to false, such as "", 0, NaN, null, undefined
- All other values will convert to true
- Identifiers, Keywords, and Reserved Words
1. Identifier
An identifier is a name given by developers to variables, properties, functions, or parameters. Identifiers cannot be keywords or reserved words.
2. Keywords
Keywords are words already used by JavaScript and cannot be used as variable or method names. Examples include: break, case, catch, continue, default, delete, do, else, finally, for, function, if, in, instanceof, new, return, switch, this, throw, try, typeof, var, void, while, with, etc.
3. Reserved Words
Reserved words are words that are current not keywords but may become keywords in the future. They cannot be used as variable or method names. Examples include: boolean, byte, char, class, const, debugger, double, enum, export, extends, final, float, goto, implements, import, int, interface, long, native, package, private, protected, public, short, static, super, synchronized, throws, transient, volatile, etc.
- Operators
Operators, also known as operators, are symbols used to perform tasks like assignment, comparison, and arithmetic operations.
Common operators in JavaScript include:
- Arithmetic operators
- Increment and decrement operators
- Comparison operators
- Logical operators
- Assignment operators
1. Arithmetic Operators
With y=5, the following table explains these arithmetic operators:
| Operator | Description | Example | x Result | y Result |
|---|---|---|---|---|
| + | Addition | x=y+2 | 7 | 5 |
| - | Subtraction | x=y-2 | 3 | 5 |
| * | Multiplication | x=y*2 | 10 | 5 |
| / | Division | x=y/2 | 2.5 | 5 |
| % | Modulus (remainder) | x=y%2 | 1 | 5 |
| ++ | Increment | x=++y | 6 | 6 |
| ++ | Post-increment | x=y++ | 5 | 6 |
| -- | Decrement | x=--y | 4 | 4 |
| -- | Post-decrement | x=y-- | 5 | 4 |
2. Comparison Operators
| Operator | Description |
|---|---|
| == | Equal to |
| === | Strictly equal (value and type are both equal) |
| != | Not equal to |
| !== | Not strictly equal (either value or type is different, or both are different) |
| > | Greater than |
| < | Less than |
| >= | Greater than or equal to |
| <= | Less than or equal to |
3. Logical Operators
| Operator | Description | Example |
|---|---|---|
| && | and (both conditions must be true) | (x < 10 && y > 1) is true |
| || | or (at least one condition must be true) | (x==5 || y==5) is false |
| ! | not (reverses the logical state) | !(x==y) is true |
Short-circuit Evaluation (Logical Short-circuit)
Short-circuit evaluation works by stopping further evaluation of expressions once the result is determined. For example:
- Logical AND
Expression1 && Expression2 && Expression3
Summary: Returns the first expression that evaluates to false; otherwise, returns the last expression.
- Logical OR
Expression1 || Expression2 || Expression3
Summary: Returns the first expression that evaluates to true; otherwise, returns the last expression.
4. Assignment Operators
Given x=10 and y=5, the following table explains the assignment operators:
| Operator | Example | Equivalent To | Result |
|---|---|---|---|
| = | x=y | x=y | x=5 |
| += | x+=y | x=x+y | x=15 |
| -= | x-=y | x=x-y | x=5 |
| *= | x*=y | x=x*y | x=50 |
| /= | x/=y | x=x/y | x=2 |
| %= | x%=y | x=x%y | x=0 |
5. Operator Precedence
| Priority | Operator | Order |
|---|---|---|
| 1 | Parentheses | () |
| 2 | Unary Operators | ++ -- ! |
| 3 | Arithmetic Operators | First *, /, % then +, - |
| 4 | Relational Operators | > >= < <= |
| 5 | Equality Operators | == != === !== |
| 6 | Logical Operators | First && then || |
| 7 | Assignment Operators | = |
| 8 | Comma Operator | , |
- Logical not in unary operators has a high priority.
- Logical AND has higher precedence than logical OR.