Come in and have a look, let you lie down and finish learning JavaScript (2)


JavaScript basic syntax 2

If you want to be an excellent Java Architect, see here → Le byte free open class (Tencent class)

If you need more information, please click on the right → this is a road of no return, there is a risk of baldness, please choose carefully!

Statements and comments

The execution unit of JavaScript program is line, that is, line by line. In general, each line is a statement.

Statement is an operation to complete a certain task. The statement ends with a semicolon, and a semicolon means the end of a statement. Multiple statements can be written on one line (this is not recommended), but when multiple statements are written on a line, the statements must end with a semicolon.

The expression does not need to end with a semicolon. Once you add a semicolon to the expression, the JavaScript engine treats the expression as a statement, which produces statements that don’t make any sense.

Single line comments: start with // and;
Multiline comments: put between / * and * /.
Compatible with HTML annotation: <! --- >

Identifiers and keywords

identifier Is a name, used to name variables and functions, with specific rules and specifications


By Unicode letters_ , $, number composition, Chinese composition
(1) Cannot start with a number
(2) Cannot be keyword and reserved word
(3) Case sensitive


(1) See the name and know the meaning
(2) Hump naming or underlining rules

keywordAlso known as reserved words, are words that JavaScript uses to have special meanings



A variable is a memory space with a name to store data. Data can be stored in variables or extracted from variables.

Declaration of variables

JavaScript is a weakly typed language. When you declare a variable, you do not need to specify the data typevarModifier.

Variable declaration and assignment:

//Declaration before assignment
var a ;    
a = 10;
//Declare simultaneous assignment
var b = 20;

Notes on variables

(1) If it is only declared but not assigned, the value of the variable is undefined.

var box;

(2) Variables can only be used if they are defined. If variables are used without declaration, JavaScript will report an error and tell you that the variables are undefined.


(3) Multiple variables can be declared in the same var command.

var a, b, c = 10;

(4) It is invalid to use VaR to re declare an existing variable.

var box = 10
var box;

(5) If VaR is used to re declare an existing variable and assign a value, the previous value will be overridden

var box = 10;
var box = 25;

(6) JavaScript is a dynamically typed and weakly typed language, that is to say, there are no restrictions on the types of variables, and various types of values can be assigned.

var box = 'hello world';
box = 10;

Variable promotion

The JavaScript engine works by parsing the code, getting all the declared variables, and then running it line by line. As a result, all variable declaration statements will be promoted to the head of the code, which is called variable promotion.

var msg = "so easy";

//Variable promotion, equivalent to the following code
var msg;
msg = "so easy";

//Note: the final result is undefined, indicating that the variable MSG has been declared but not assigned.

Note: variable promotion is only valid for variables declared by VAR command. If variables are not declared with VaR command, variable promotion will not occur.

msg = "error";

data type

Although JS is a weakly typed language, variables have no type, but the data itself is typed. For different types, we can carry out different operations.

There are six data types in JavaScript, including five simple data types:UndefinedNullBooleannumerical valueandcharacter string。 A complex data typeObject

Number: integers and decimals (such as 1 and 3.14)
String: text composed of characters (such as "Hello world")
Boolean: two specific values: true and false
Undefined: it means "undefined" or does not exist, that is, there is no value at present
Null: indicates a vacancy, i.e. there should be a value here, but it is currently empty
Object (Reference): a collection of values
    1) , object {Name: "Zhangsan", age: "18"}
    2) , array [1,2,3]
    3) , function (function) function test() {}


The value of type undefined is undefined.

Undefined is an original value for none, which means that the value does not exist.

Common cases of undefined are as follows:

(1) when a variable is declared but not initialized, the value of the variable is undefined

var box;
console.log(box); //undefined

(2) when calling a function, if the function has a formal parameter but no argument is provided, the parameter is undefined.

Function NoData (STR) {// JS function parameters only need variable names
    console.log(str); // undefined
Nodata(); // when calling the method, no parameters were passed

(3) if the function does not return a value, it returns undefined by default.

//Method has no return value
function noData() { 
Var re = nodata(); // defines the method of variable receiving no return value


Null type is a data type with only one value, that is, the special value null. It represents a null value, that is, the value at this point is now empty, and it represents an empty object reference.

Note the following when using null type values:

1) use the typeof operator to test null and return the object string.

2) undefined derives from null, so the return value of equivalence comparison is true. Uninitialized variables are equal to variables assigned null.

console.log(undefined == null);
Var box = null; // variable assigned to null
Var a; // uninitialized variable
console.log (a = = box); // the two values are equal

Boolean type

Boolean types have two values: true and false. It is often used as a condition for judgment and circulation

Numerical type

Numeric types contain two types of values: integer and floating point.

1) all numbers (integer and floating point) are stored as 64 bit floating-point numbers. So, in JS, 1 is equal to 1.0, and 1 Plus 1.0 is still an integer. The highest precision of floating-point number is 17 decimal places. As floating-point number operation may not be accurate, try not to use floating-point number for judgment.

2) when storing numerical data, the floating-point value that can be converted into integer type will be automatically converted into integer type.

console.log(1 == 1.0); // true
console.log(1 + 1.0); // 2
Var num = 8.0; // automatically converts floating-point numbers that can be converted to integers
console.log(num); // 8

character string

Use ” or “” to lead, such as’ hello ‘,’ good ‘.

Use the plus sign ‘+’ for string splicing, such as: console.log (‘hello’ + ‘ everybody’);


An object is a collection of data and functions.


{}: represents an object defined in the literal form of an object. Empty curly braces denote the object that defines the default properties and methods.