What is JavaScript?
JavaScript is an object-based, event-driven scripting language with security features.
The official name of JavaScript is "ECMAScript" (ECMA is the European Computer Manufacturers Association).
JavaScript Features:
- A scripting language
- Object-based
- Dynamic
- Simple and easy to use
- Security
- Cross-platform
- Improving user experience
JavaScript scripting language, like other languages, has its own basic data types, expressions, arithmetic operators, and basic program framework. JavaScript provides four basic data types and two special data types to handle data and text. Variables provide a place to store information, while expressions can accomplish more complex information processing.
Differences between JavaScript and Java
Java code must be compiled before execution, while JavaScript does not need compilation and only needs to be interpreted and executed by the browser.
Although both Java and JavaScript can execute on the server and client, Java mostly runs on the server, while JavaScript mostly runs on the client.
JavaScript uses loose data types, while Java uses strict data types.
JavaScript Data Types
- Basic Data Types (3 types)
- Composite Data Types (2 types)
- Special Data Types (2 types)
Composite Data Types
- Array
- Object
Special Data Types
- null (Empty)
- undefined (Undefined)
Variable Naming Rules
- Variable names can only contain uppercase and lowercase English letters, digits, $, and underscore, but cannot start with a digit.
- Case-sensitive. Example: A and a are two different variables.
- Invalid names: my-name, 76person, 007
- Valid names: $wu, var_name, _dum, v108
Variable Declaration
- Use var to declare.
There is no need to declare the data type of a variable; its type is determined when used or assigned.
var a = 18 ; //a为数值型 var b = "tom" ; //b 为字符串 var c = true ; //c为布尔型
- Global variables and local variables
- Local variables are declared inside functions and must be declared using var.
- Global variables are declared outside functions and do not need to be declared using var.
Special Operators

Conditional and Loop Control Statements
JavaScript statements are used to implement basic program control and operation functions.
- if Conditional Selection Statement
- switch Selection Statement
- do...while Statement
- while Loop Statement
- for Loop Statement
- for(...in...) Statement
- break Statement and continue Statement
if Selection Statement
Conditional Selection Statement 1:
if(expression)
{
statements
}
Conditional Selection Statement 2:
if(expression)
{
statements
}
else
{
statements
}
Conditional Selection Statement 3:
if(expression1)
{
statements
}
else if (expression2)
{
statements
}
else if (expression3)
{
statements
}
else
{
statements
}
switch Selection Statement
switch(表达式)
{
case:语句
break;
case:语句
break;
……
default:语句
}
do...while Statement
First execute a statement, then repeatedly execute it in a loop until the conditional expression is false.
do
{
语句
}
while(条件判断);
while循环语句
while语句执行的时候,直到指定的条件为false为止。其用法如下:
while(条件)
{
语句
}
for Loop Statement
The for statement executes a loop of statements until the condition is false.
Usage is as follows:
for([初始表达式];[条件];[增量表达式])
{
语句
}
for(...in...) Statement
For each property of an object, or each element of an array, execute one or more statements.
Usage is as follows:
for (variable in [object | array])
{
语句
}
break and continue Statements
The break statement terminates the current while, for, and do...while loops, directly exiting the loop and executing the statement after the loop.
The continue statement ends the current iteration of the loop.